Search code examples
javauser-interfacejframejpanelflowlayout

Java JPanel/GUI contents appear only sometimes when run


i'm new to Java programming, but I've done C++ for a while now. the whole GUI thing is new to me. i created two JPanels and added them to JFrame using FlowLayout. When I run the program, i get http://imageshack.us/photo/my-images/43/36213853.jpg/ as opposed to http://imageshack.us/photo/my-images/88/86682510.jpg/ only SOME of the times. the other times, the content appears just fine for some reason, when i resize the window when the window is blank, the contents appear fine.

i'm not sure what the problem is that gives me this inconsistency!

any help would be appreciated. thanks!


Solution

  • It's hard to answer without code, but by chance are you calling setVisible(true) before packing your GUI and before adding components to the GUI? Do the components show up if you resize your GUI?

    If so, you'll want to make this call only after your GUI has been fully populated with components, and usually after it has been packed (so that your layout managers can lay out all components and size the GUI correctly).

    If this advice doesn't help, then you'll likely need to post some of the offending code. How much to post -- it's hard to say -- enough so that we can identify the problem ;) but not to much as to be drowned in code not related to your problem. The best code to post is an SSCCE if you have the ability to create one.