Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a ScrollPane resize along with its parent Frame  RSS feed

 
Peter Desjardins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I'm writing a game user interface using a Canvas that represents a game map. The Canvas is displayed inside a resizable Frame. Since the map can be larger than the Frame I want to use scrollbars so users can scroll to different areas of the map.

I had this working with JFrame, JPanel, and JScrollPane after I got some advice about Swing and threads in this forum. Then I switched to java.awt.Frame and java.awt.Canvas in order to use some code that handles buffer strategies and the game loop that repaints the interface.

I've got some working code that places the Canvas in a java.awt.ScrollPane and the ScrollPane in the Frame. However, the ScrollPane does not resize along with the Frame. I've only been able to give the ScrollPane an explicit height and width.

Is there a way to make the ScrollPane resize along with its parent Frame?

Is there a better choice of classes for my frame, scroll bar, and game UI? Suggestions would be welcome.

Thanks for your help. My JDK version is 1.6.0_24.

Peter
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What layout are you using?
Usually, a container with a BorderLayout with the JScrollPane added to the center gives best results, as far as the scrolling and resizing issues are concerned.
 
Peter Desjardins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. Frame's layout was null.

I removed the explicit size of the ScrollPane and then changed this:

frame.setLayout(null);

to:

frame.setLayout(new BorderLayout());

Now the ScrollPane resizes along with the parent Frame. Thanks!

Peter
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A frame (or more technically, a frame's content pane) already has a BorderLayout by default. That means that you can remove that line completely, and it will work the same way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!