• 11
• 27
• 9
• 20
• 31

[java] Java swing components don't always draw

This topic is 2917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

What might cause a swing component (anything from a JFrame to a JPanel to a JMenu) to fail to draw when it appears? I create various components in various places (only in the AWT-Thread though), and simply creating them makes everything Just Work on my machine (JDK 1.6). However, when I attempt to run the project at school (JDK 1.5), various parts of the GUI fail to draw until triggered. For example, the main window refuses to draw unless I manually resize it by dragging on a corner with my mouse (and, yes, I'm repeatedly calling repaint() but it's not helping). Another example, creating a JPopupMenu and adding a bunch of JMenu's to it appears, but the JMenu's aren't drawn unless I mouse over them. Again, this is only on the school computers. Write-once-run-anywhere my ass... I'd use C# but I'm forced to use Java for this project, so any help at all would be highly appreciated!

Share on other sites
did you pack the layout?

Share on other sites
Quote:
 Original post by flerydid you pack the layout?

No; do I have to pack it even for basic operations, like adding a JMenu to a JPopupMenu?

Share on other sites
You have to call the validate() method of a component that contains any component you add. Manually resizing the window happens to call that method automatically. The pack() method is meant to set the size of the window to fit the components added to it.

Share on other sites
Quote:
 Original post by DathgaleYou have to call the validate() method of a component that contains any component you add. Manually resizing the window happens to call that method automatically. The pack() method is meant to set the size of the window to fit the components added to it.

That's really weird, I don't see any of the Java docs examples doing this :( And it works great on my computer (jdk 1.6), it just fails to render properly on my school computers (jdk 1.5).

Share on other sites
Quote:
 Original post by nullsquaredThat's really weird, I don't see any of the Java docs examples doing this :( And it works great on my computer (jdk 1.6), it just fails to render properly on my school computers (jdk 1.5).

They do show this in the official tutorial trail. Try following those steps exactly and see if it works on both 1.5 and 1.6.

Share on other sites
Silly question, but are you compiling at school. I have written 5.0 code before, but compiled and built a jar file at home with 6.0. Because it was not compiled to 5.0, that could cause so weird issues...

Share on other sites
Quote:
Original post by nullsquared
Quote:
 Original post by DathgaleYou have to call the validate() method of a component that contains any component you add. Manually resizing the window happens to call that method automatically. The pack() method is meant to set the size of the window to fit the components added to it.

That's really weird, I don't see any of the Java docs examples doing this :( And it works great on my computer (jdk 1.6), it just fails to render properly on my school computers (jdk 1.5).

The only time you don't need to call validate() is when you add a component to another component that isn't visible yet, which I bet is what the examples you saw did.

Quote:
 Original post by Glass_KnifeSilly question, but are you compiling at school. I have written 5.0 code before, but compiled and built a jar file at home with 6.0. Because it was not compiled to 5.0, that could cause so weird issues...

It's doubtful that this is the problem. Class files don't run at all on a JVM that is earlier than the version of Java that they are compiled for. Also, the Java standard libraries behave the same from version to version to allow class files compiled for earlier versions of Java to run as they were originally designed to.

Share on other sites
A very good idea (if deploying for older java versions than your own) is to get the JDK for that version and use it for compiling and running. For example, use the 5.0 JDK on your home computer for testing the program instead of 6.0, this way you can find whatever compatibility issues there may be before you distribute the app.

Share on other sites
OK, I called validate() on every component I added components to, and...

nothing.

Same issue. Runs fine on my computer, fails to render properly on the school computers. It applies to:
- JMenuItems inside a JMenu inside a JPopupMenu (sometimes renders right, other times is gray until I mouse-over something, causing a repaint())
- JDialog with stuff inside it (I need to resize it manually with my mouse to get it to show stuff)
- JLabel at BorderLayout.SOUTH under my main game-content JPanel (which is at BorderLayout.CENTER)

Extra info: in my main loop I have to call:
content.repaint(); // the JPanel with the overridden repaint()frame.repaint(); // the JFrame

If I don't call content.repaint(), even my game-content doesn't display right. I thought calling frame.repaint() would be enough.