File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem in loading swing GUI components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem in loading swing GUI components " Watch "Problem in loading swing GUI components " New topic

Problem in loading swing GUI components

Aos Khalifa

Joined: Mar 18, 2012
Posts: 5
Hello guys,
I have this problem , When i run my java app ,sometimes it doesn't load GUI components till i focus it or move the mouse over it .
ex : I have a JButton , sometimes it appears correctly and sometimes I have to move the mouse over it to be displayed .

any solution please ??

Thanks .....
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

You probably need to show a simple example (the absolute minimum required to reproduce the problem). My guess is that you are putting up your main frame, then building the area that has your JButton in it in the main thread (or at least not the EDT/GUI thread), and so the GUI doesn't see it/respond to its presence until it is forced to re-paint that area of the screen. All your GUI code should be done in the Event Dispatch Thread (EDT/GUI thread). SwingUtilities has a few methods (invokeLater() and invokeAndWait()) which may help you with this.

All that is a guess, though. We need to see your code to do anything but guess.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46383
Always ensure the setVisible(true) call appears after all add method invocations, setSize, etc., etc.

I think this discussion would fit better in our GUIs forum, so I shall move it thither.
I agree. Here's the link:
subject: Problem in loading swing GUI components
It's not a secret anymore!