Sign in to follow this  
Chryzmo

[java] Resizing JScrollPane

Recommended Posts

Hello all, I am writing a 2D map editor for my game. I am using Java Swing for the map editor, although the game itself will probably be written using C++ and SDL. I have a 2D array of MapTiles that contain the information of each tile in the map, if I start the program with a default of, say, 30x30 tiles, the JScrollPane has a scroll bar and it works correctly. If I click the new map button and resize the map to 5x5, thus not needing the scroll bars, they disappear and everything works properly. When I resize the map a larger size than the default starting size, the MapTile array is resized correctly, however the JScrollPane does not resize correctly. However, if I maximize, then restore the window, the JScrollPane will resize itself correctly. Is there someway I can do this automatically in the code when I want to resize the map? Thanks, Chris Moore

Share this post


Link to post
Share on other sites
http://java.sun.com/docs/books/tutorial/uiswing/TOC.html

Bookmark the above link.

I'm assuming you have an extended Jpanel in the Jscrollpane?

In which case you should do the following:

*Make sure the Jpanel implements scrollable
*When the Jpanels required size changes call setPrefferedSize on the Jpanel. getPrefferedViewportsize should probably just return getPrefferedSize (the same size you just set)
*To force JScrollBar to update when the above has occured call revalidate on the jpanel

I hope that helps...


[edit]
Before I forget, theres also a nice Java/Swing Opensource mapeditor here: http://mapeditor.org/

Share this post


Link to post
Share on other sites
Awesome! I had been trying to call revalidate on the JScrollPane, but calling it on the JPanel instead worked. Thanks a lot :-D

And I glanced at the map editor. I wanted to make this one, just to as a personal project, but I'll download the one you listed and check it out. Mine is very simple, so I would probably benefit more from a better made editor.

Thanks again,
Chris

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this