Trivial program flickers on startup.

The following program flickers on startup - the field is sometimes displayed, then erased, then displayed again.
From the output to the error stream, it appears that the field is being painted, before the frame has even been. It's not surprising then that it gets erased when the frame is painted for the first time. This happens after the frame is made visible.
Indeed. I've got around it with a hack which consists of having JTextField's paint method ignore the request if the frame hasn't been painted.
But why does this happen?
import javax.swing.*;
import java.awt.Container;
import java.awt.Graphics;
public class Flicker implements Runnable {
     public static void main(String args[]) {
          SwingUtilities.invokeLater(new Flicker());
     public void run() {
          JFrame jf = new MyFrame();
          jf.setSize(800, 800);
          Container jfcp = jf.getContentPane();
          jfcp.setLayout(null);
          JComponent jtf = new MyTextField();
          jtf.setLocation(500, 500);
          jtf.setSize(jtf.getPreferredSize());
          jfcp.add(jtf);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          System.err.println("Setting frame visible");
          jf.setVisible(true);
     class MyTextField extends JTextField {
          @Override
          public void paint(Graphics g) {
               System.err.println("JTextField paint called");
               super.paint(g);
          MyTextField() {
               super("Field text");
     class MyFrame extends JFrame {
          public void paint(Graphics g) {
               System.err.println("JFrame paint called");
               super.paint(g);
          public void update(Graphics g) {
               System.err.println("JFrame update called");
               super.update(g);
}

OK, FWIW, what I've surmised is this. When the JFrame is set visible, a PaintEvent is posted to the event queue. Then, when the swing component's repaint methods are called, the RepaintManager posts an InvocationEvent to run some code that will call the components' paint(Graphics) methods.
The problem is that InvocationEvent events have a higher priority than PaintEvent events, so when the code constructing the component graph completes, the first event that gets dispatched is the one for the RepaintManager, which accordingly paints the components. Then finally the paint event is dispatched, and the frame paints itself and its children.
A workaround, of sorts, is to set the frame visible as soon as it has been constructed and had its size and position set, and then to defer the rest of the code to a point after the PaintEvent is dispatched. This can be done using an AwtEventListener, as shown below. The major downside is that adding such a listener may be prevented by a SecurityManager.
It becomes more fun when a layout manager is used for the frame, because it cannot be sized until all its descendant components have been added. The best I can think of is to add them all, pack the frame, then remove them again (or just their common ancestor), set the frame visible, and again wait for the paint event before adding them back.
I can only assume that everyone out there is using such fast machines that the flicker is not visible.
Sylvia.
import javax.swing.*;
import java.awt.AWTEvent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.Component;
import java.awt.event.AWTEventListener;
import java.awt.event.PaintEvent;
public class Flicker implements Runnable {
     public static void main(String args[]) {
          SwingUtilities.invokeLater(new Flicker());
     public void run() {
          final JFrame jf = new MyFrame();
          jf.setSize(800, 800);
          jf.setVisible(true);
          new MyAwtEventListener(jf, new Runnable() {
               public void run() {
                    secondPart(jf);
     private void secondPart(JFrame jf) {
          Container jfcp = jf.getContentPane();
          jfcp.setLayout(null);
          JComponent jtf = new MyTextField();
          jtf.setLocation(500, 500);
          jtf.setSize(jtf.getPreferredSize());
          jfcp.add(jtf);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          System.err.println("Setting frame visible");
          jf.setVisible(true);
     static class MyAwtEventListener implements AWTEventListener {
          private final Runnable runnable;
          private final Component component;
          MyAwtEventListener(Component component, Runnable runnable) {
               this.runnable = runnable;
               this.component = component;
               Toolkit.getDefaultToolkit().addAWTEventListener(this, PaintEvent.PAINT_EVENT_MASK);
          public void eventDispatched(AWTEvent event) {
               if(event.getID() == PaintEvent.PAINT && event.getSource() == component) {
                    Toolkit.getDefaultToolkit().removeAWTEventListener(this);
                    SwingUtilities.invokeLater(runnable);
     class MyTextField extends JTextField {
          @Override
          public void paint(Graphics g) {
               System.err.println("JTextField paint called");
               super.paint(g);
          MyTextField() {
               super("Field text");
     class MyFrame extends JFrame {
          public void paint(Graphics g) {
               System.err.println("JFrame paint called");
          public void update(Graphics g) {
               System.err.println("JFrame update called");
               super.update(g);
}

Similar Messages

  • I have Photoshop Elements 11 installed on my PC, and now it flickers on startup of the Editor, then closes on its own with a message saying it has stopped working.

    Photoshop Elements 11 has been working for over a year, and now suddenly it flickers on startup, then closes with a message saying it has stopped working.
    Is there a repair option to fix it?

    I have already copied my photographs to the a library manager but when I tried to reuse iphoto and import the photos back into iphoto, it would not let me.
    You can't "copy" your photos to the "library manager", there is just no way to any such thing.
    Option 1
    Back Up and try rebuild the library: hold down the command and option (or alt) keys while launching iPhoto. Use the resulting dialogue to rebuild. Choose to Repair Database. If that doesn't help, then try again, this time using Rebuild Database.
    If that fails:
    Option 2
    Download iPhoto Library Manager and use its rebuild function. (In early versions of Library Manager it's the File -> Rebuild command. In later versions it's under the Library menu.)
    This will create an entirely new library. It will then copy (or try to) your photos and all the associated metadata and versions to this new Library, and arrange it as close as it can to what you had in the damaged Library. It does this based on information it finds in the iPhoto sharing mechanism - but that means that things not shared won't be there, so no slideshows, books or calendars, for instance - but it should get all your events, albums and keywords, faces and places back.
    Because this process creates an entirely new library and leaves your old one untouched, it is non-destructive, and if you're not happy with the results you can simply return to your old one.  
    Regards
    TD

  • 2008 Mac Pro 10.5.8 shuts down after hours of no use, Set to never shut down. Can be resumed pressing any key but all programs not in startup list need to be restarted and unsaved data lost .   New behavior.  No system changes.

    2008 Mac Pro 10.5.8 shuts down after hours of no use, Set to never shut down. Can be resumed pressing any key but all programs not in startup list need to be restarted and unsaved data lost .   New behavior.  No system changes.

    2008 Mac Pro 10.5.8 shuts down after hours of no use, Set to never shut down. Can be resumed pressing any key but all programs not in startup list need to be restarted and unsaved data lost .   New behavior.  No system changes.

  • Photoshop - program flickers

    Hi all
    I have a relatively new computer, all the correct drivers etc installed, spec:
    Windows 7 Ultimate
    Intel(R) Core(TM)2 Quad CPU @ 2.50GHz
    4GB RAM
    64 Bit Operating System
    Graphic Cards: NVIDIA GeFORCE 8400 GS
    ..yet my computer seems to have trouble handling Photoshop (version CS3 Extended) inparticularly but also had similar problems with Bodypaint and Adobe Professional, though no problems with Maya.
    The problem I'm experiencing with Photoshop: whilst initiating a simple command such as 'clone' or even saving a file and trying to change it's file type the program flickers, the tool bar and other command bars disappear for a split second and then come back, the flickering stops once the command has been completed. As you can imagine this makes it's pretty much impossible to work on.
    ....any ideas what's going on!?...help much appreciated!

    Video card has been updated, also updated Photoshop, still have the problem.
    I have done my own research to find a solution, thought it was something to do with NVIDIA SLI option, but then found out that's only if you have more than 1 graphic card.... so I am stumped and seriously frustrated!

  • OS X - on startup iphoto and excel start up casuing delays. How do I control programs at upon startup

    OS X - on startup iphoto and excel start up causing delays. How do I control programs at upon startup
    Thanks for any assistance.

    sys / prefs /users and groups. if iphoto and excel are present in the login items then simply check the sidebar as to prevent them starting up.
    if they are not simply add them and do the above.

  • Running java program at windows startup

    I need to run java program at startup and then it will continue to run in its own thread.
    Any body having idea how to do that programmatically?
    Zeeshan

    This is one option but this is not feasible in my scenario as this application will be running over a network which involves hundreds of computer so is there any way to do it programatically?
    I mean if I can load java class file through an exe file then I can load my class file in the startup using C++.
    Any Suggestions.
    thanks for the help
    regards,
    Zeeshan

  • How can I add a program to the Startup program

    When I copy a program that I want to add, I don't get a Paste menu item when right-clicking Startup menu item found when clicking Start and All Programs, so I can't add it to the Startup program

    I figured out a way to do this:
    1- Copy the shortcut to the program (or the program's .exe (execute file)) you want to add to Startup
    2- Right-click Startup (after selecting it in All Programs) and select Explore
    3-In the file structure that opens, right-click Startup
    4-Click Save.
    P.S. '''alan_r''' Thanks for trying but your suggestion didn't solve it.

  • Prevent elevation message for some programs at windows startup

    Hi
    I'm using Windows 8.1 in a 2012 R2 Domain.
    When my main computer starts, 3 Programs require elevation to run.
    When I logon my adminstrator account I know the password but for my non administrator user, it's an issue.
    Is it possible to setup startup of those programs to run without elevation or to type the password once for all lauches ?
    Thanks for your answers,
    Laurent

    Hi,
    In this situation, it means when you want to run this kind of applications, you need the administrative token, basically, this is designed by the application provider:
    Running with Administrator Privileges
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms717801(v=vs.85).aspx
    Alex Zhao
    TechNet Community Support

  • Flash CS3 program flickers during "undo" and "save and compact"

    Hi,
    I have Flash CS3 Suite. I have a computer that was built in 2004, running Windows XP Professional - 2 Gb RAM although I have given the Virtual memory 4 Gb. The reason I did that is because I thought maybe my RAM wasn't enough. I have no problems with any of the other Adobe programs or any other programs on my computer, just Flash Professional. The running process only uses 16,030 K so it can't be that.
    I have uninstalled, cleaned all adobe files completely off of my computer, done a fresh install, more than once, but no matter what, I seem to have issues with the 2 following commands:
    1.  Edit > undo feature
    2.  File > save and compact - although this doesn't flicker as much as the other one.
    The flickering is frustrating, and, of course I can't do anything else until the flickering stops.
    The positive side of this issue is that I am being much more thoughtful about my editing which is good anyway, but it would be nice for the flickering to permanently stop. Any and all help will be greatly appreciated.
    jd

    upload your fla and post a link.

  • Autorun a program on computer startup

    Hi all,
    I have a doubt of how can I make a program autorun on windows startup (we'll leave Linux for later).
    I have followed two ideas but in both cases I find the same problem.
    - The first idea was to put an Icon on Start Menu / Programs / Startup. For this I have to search where this folder is and, to find it, I have to read the Windows registry.
    - The second idea was to write directly on Windows registry so the program will start automatically on windows startup.
    So to read or write the windows Registry I have to use JNIRegistry API (because Java itself has no other class to do it, instead a class that can read some certain points in the Registry that I don't need). I can import their classes (JNIRegistry) using a jar and develop with it. But a DLL it's needed too and I want to avoid them because I don't want to install DLLs on customers computers (this program is going to be distributed).
    So, is there any other way to do these things avoiding the DLL thing or the need to read the Registry to find where de StartUp folder is?
    Thanks in advance and if you don't understand it I'll try to explain better ;-)

    Sorry, I thought that maybe it was not a especifically Swing thing and I have copied it here.
    Delete it if you find it repetitive.

  • Why is Explorer.exe reading my program files at startup?

    Every time I boot my computer, if I open Resource Monitor, I will inevitably find, under the "Disk Activity" section, the process explorer.exe "reading" my program files.
    When I say "program files", I mean the executable files for applications stored in the "C:\Program Files (x86)" or the "C:\Program Files" directories.
    Other files seem to get "read" a lot too. PNF files for example.
    I have indexing turned OFF on all drives, and yet the process SearchIndexer.exe is almost always running too.

    Hi,
    Firstly, did you installed MSE in your system, this phenomenon can be caused by MSE security scanning disk.
    Secondly, this problem can also be caused by 3rd program, it would be better to use AutoRuns to check system startup items if there is any 3rd process or dll file start with system.
    AutoRuns:http://technet.microsoft.com/en-us/sysinternals/bb963902.aspx
    Roger Lu
    TechNet Community Support

  • ALL programs gone at startup C655D-S5228 satelitte

    New laptop,sep 2011,took all day loading games,AVG, Zonealarm, other CD's,screensavors. Updated all, desktop the way I wanted, for trip to NM Thanksgiving. In morn,turned on, said it was recovering startups, had to wait. It ERASED ALL THE NEW PROGRAMS I INSTALLED, WHY? Live in OKC,traveled to NM for Holidays. Have Warranty, recovery disks, but they are at home, no good here. Bought at Office Depot, will call them here. Thanking you all kindly for any help you can give to me, Helene.

    Thank you for your help, since we are now back in OKC, will take the laptop into shop, it is under warranty, only had it for 2 mths now. I kept it plugged into, and online using daughters wireless connection while in NM, plugged right into the car, keeping a few things I had re-downloaded in NM; it was FUN playing a few games on our 7 hr drive back home! I am sure Office Depot Tech will figure out whats up, and I already had the 4 recovery disks for him to use if he has to, and then will spend time again loading the games, etc back into it.
    One great thing about having to redo this, is that I am becoming more and more familiar with my new hardly used laptop, so all is not a waste, right? Thank you again, God Bless! Helene

  • Program autostarts at Startup

    I have a new Bose SoundTouch music system and have downloaded the SoundTouch app to my Mac. The app autostarts every time I reboot the system. I would like it only to start when I want it to run. I have checked my account Login items, and the app is not listed there. I have checked the startup folder in Library, and it is not listed there either. I have uninstalled the program using the Bose SoundTouch uninstaller and reinstalled the latest version of the program with no luck. I have searched out and removed every SoundTouch plist file I can find. No joy. Where else can I look? There is something hidden somewhere in OS X that is causing this to happen and I would like to find and remove it. I have contacted Bose and they are nice people to deal with but keep telling me that this is not supposed to happen with this app. The app has no preferences or settings to control startup at log in. They say this is an OS X issue. Any help would be appreciated.

    You will need to make your user library visible. Go to Finder and select your user/home folder. With that Finder window as the front window, either select Finder/View/Show View options or go command - J.  When the View options opens, check ’Show Library Folder’. That should make your user library folder visible in your user/home folder.
    Thanks to leonie for some information contained in this.
    You might need to redo the View settings after an update.

  • Program loads at startup

    How can I change a program or Safari so that it will not load at startup? I have a password app that I want to have not load while I test another!
    Thx

    First check System Preferences - Users & Groups and check to see Safari is not in the Startup apps, if it is then select it and click the - symbol below it. If it still starts then uncheck the box when you shut down that asks if you want all apps left open to re-start when you start up.
    However why in the world are you shutting it down, simply put the machine to sleep or display to sleep (my favorite) and let the machine run 24/7, after nearly 8 years of Mac use that has been extremely stable for me.

  • Starting Program on Computer Startup

    I have an executable LabView program that I would like to start running automatically when the computer is started. How can I do this? Thanks for the help.

    In order to answer your question a few more details are needed. What is your OS? Are you using some flavor of Windows or some other OS? If Windows then you can start the program at login by including a shortcut in the Startup folder. You can access this by right clicking on the start button and clicking open and then going to the startup folder (which will probably be under the programs folder). Depending on the flavor of Windows you may be able to set the program to start for some users and not for others or to start for all users.

Maybe you are looking for

  • Mail sounds no longer working

    After upgrading to Mountain Lion, the Mail app (6.0) no longer makes sounds for checking, sending, or receiving email messages.  Sounds are enabled in both Mail preferences as well as the mail section of Notifications.  Sounds elsewhere still work, j

  • Change Agents determination

    Hi Everyone, I've copied a standard workflow to a custom one and now I have to change how the approvals will be determined. I'm trying to do that changing the workflow container through a programmed bindind. I know that it's also possible to change t

  • Native application installer fails on Mac 10.5.8 every other time

    Hello, I have a native AIR 2.0 application with a native installer which I silently install as a part of a bigger package. On Mac, I have a DMG which contains the native installer app and a shell script which copies the native installer app into a te

  • Connecting a Windows formatted ipod to a Mac

    My ipod used to be connected to a PC, but since a couple of days i got a mac; and when i plug it in, itunes recognizes it but then freezes. I cant use itunes anymore or access my ipod unless i unplug it. i tried quickly restoring my ipod before it fr

  • OMG PLEASE HELP ME! WON'T START UP AND SCREEN LOOKS FUNNY O.O

    Okies, my sister was on my macbook pro all night, and i just finished updating some program (the mac version of winzip?) and the comp froze and had little rectangles all over it, like tiny TVs with bad reception on it O.o So i hold down the Power but