my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes JScrollPane intermittently fails to display JPanel contents - gray panel instead Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane intermittently fails to display JPanel contents - gray panel instead" Watch "JScrollPane intermittently fails to display JPanel contents - gray panel instead" New topic
Author

JScrollPane intermittently fails to display JPanel contents - gray panel instead

Peter Desjardins
Greenhorn

Joined: Jun 13, 2011
Posts: 9
Hi. I'm very new to Java so this is a beginner question. I'm using a JScrollPane to view a large rectangle (a JPanel I'm calling gamepanel) in a smaller JFrame. My JDK version is 1.6.0_24 and I'm running my program locally from a JAR file.

The problem is that some of time, the "gamepanel" object and scroll bars are visible when the program runs and sometimes the screen shows a gray background with no scroll bars. It seems to be equally likely that I'll see the gamepanel or the gray background when the program runs. If it's the gray background, I can resize the JFrame and the gamepanel and scroll bars will immediately appear. If I skip the JScrollPane and add the gamepanel directly to the JFrame, I never see the gray background.

How can I adjust the way I'm using JScrollPane so that the gamepanel and scrollbars are always shown in the JScollPane when the program runs?

Thanks for your help. Here's a simplified version of my code:

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Hi Peter, welcome to CodeRanch!

Try setting the size of the frame after you add the scroll pane to it.
Peter Desjardins
Greenhorn

Joined: Jun 13, 2011
Posts: 9
That did work. Thanks!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

That may have temporarily resolved the problem, but the real issue is that you're not respecting Swing's single threaded rule.

Search the net for that phrase if you're not familiar with it. Very briefly, all Swing components should be constructed and their methods called only on the EDT.


luck, db
There are no new questions, but there may be new answers.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Also, you should only make the frame visible AFTER you have added all the components to the frame.
Peter Desjardins
Greenhorn

Joined: Jun 13, 2011
Posts: 9
Thanks! I read this page: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html. Now it seems much less magical.

Here's my new constructor:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You should also move lines 3-5 into the Runnable.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JScrollPane intermittently fails to display JPanel contents - gray panel instead
 
Similar Threads
Add ActionListener to JButton
display contents of a text file in a new window
Browse picture in JScrollpane !!!
JScrollPane in JInternalframe and JPanel in JScrollPane
JButton and opening new window