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 components won't display unless window is resized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "components won Watch "components won New topic

components won't display unless window is resized

Andy Ranangnai

Joined: Aug 11, 2009
Posts: 16
I'm having a really weird problem with things being displayed on a JFrame. I was about to tear my hair out trying to get components to display on a JPanel that is sitting in a JFrame, but after I accidentally resized the window, everything showed up! After doing this several more times, I confirmed that my components were invisible until I adjusted the window, making it bigger or smaller or just clicking and holding it was sufficient to cause everyhting to appear.

I decided to put it aside for now, and moved on to another project. I wanted to test out the setSelectionStart and setSelectionEnd methods of JTextField, so I threw a simple JTextField onto a JFrame, and had the same problem! I'm going to attach both programs, I can't figure out what they both have in common that is causing this. I have tried running them on different computers and operating systems and still having the same problem, I thought it might be a linux problem at first, weird stuff happens to us after upgrades or moving seemingly unrelated things sometimes. Very exasperated, help appreciated.

Here's the one that really gets me, it's so small there's hardly any room to make a mistake, yet I still found a way >.<

Here's my first project (it's a program to quiz my cousin on her lefts and rights, long story), I'm fairly new to swing so I know the buttons don't do anything ;) I'm just arranging for now



Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2446
The code should be:

If you add components to the frame after it is visible then you need to revalidate() the JPanel that you add the components to.
Andy Ranangnai

Joined: Aug 11, 2009
Posts: 16
*deep exhale* thank you. I was about to smash my computer and go live in a cave.
I agree. Here's the link:
subject: components won't display unless window is resized
It's not a secret anymore!