Advertisement Jump to content
Sign in to follow this  

[java] Resizing JScrollPane

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

If you intended to correct an error in the post then please contact us.

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

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...

Before I forget, theres also a nice Java/Swing Opensource mapeditor here:

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,

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!