• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JScrollPane intermittently fails to display JPanel contents - gray panel instead

 
Peter Desjardins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5371
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter, welcome to CodeRanch!

Try setting the size of the frame after you add the scroll pane to it.
 
Peter Desjardins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did work. Thanks!
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Camick
Ranch Hand
Posts: 2539
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you should only make the frame visible AFTER you have added all the components to the frame.
 
Peter Desjardins
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also move lines 3-5 into the Runnable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic