Jump to content
  • Advertisement
Sign in to follow this  
Vyper_uk

[java] More Swing hassles - scrollbars

This topic is 4847 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

I'm writing a tile based map editor, and I want to be able to load and remove textures dynamically into a window where they can then be selected. I have one JPanel as the window, which contains another JPanel onto which the textures are loaded (this is the only way I could find to get the layouts to look right). The JPanel which acts as the main window has a scrollbar, but when the JPanel which conatins the textures gets too big the scrollbar doesn't allow me to scroll to them. Most likely I've done something stupid, heres the code which makes the window
public TextureWindow(TileCache t) {
		textureButtons = new ArrayList();
		textureMap = new HashMap();
		tileCache = t;
		JPanel panel = new JPanel();
		panel.add(this);
		panel.setPreferredSize(new Dimension(170, 400));
		layout = new GridLayout(0, 4, 5, 5);
		setLayout(layout);
		addTex("grass1.gif");
		scrolly = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	}
The class TextureWindow extends JPanel The addTex method loads a texture and adds it to the TextureWindow, this works The JPanel is the main window, and it contains the TextureWindow that has the textures painted onto it I add the whole thing to my gui via the scrollbar (eg mainPanel.add(textureWindow.getScrolly()) Heres a sample screen of what it looks like at the moment: http://img53.imageshack.us/img53/9794/scrollwindow4pn.jpg I'd really appreciate any help you have with this, I've spent the last few days slowly working it out bit by bit but everytime I think I've got it something else goes wrong

Share this post


Link to post
Share on other sites
Advertisement
knowing absolutely nothing of your internal functions, this looks like it could cause the trouble:
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER

Share this post


Link to post
Share on other sites
No its not that, the tiles are added in rows of up to four, and the panel is big enough to display four in a row. They never go off of the side of the panel, the rows disappear off of the bottom if you get what I mean

Share this post


Link to post
Share on other sites
I usually do scrolly = new JScrollPane(panel); wich works fine, but I'm guessing your problem doesn't have anything to do with scrolling, but drawing... so how do you draw the textures? Are they added to the panel (in some way)? or simply drawn by TextureWindow?

Share this post


Link to post
Share on other sites
The textures are added like this:

public void addTex(String texture) {
//if(!textureMap.containsKey(texture)) {
TextureButton t = new TextureButton(tileCache, texture);
textureButtons.add(t);
textureMap.put(texture, t);
add(t);
//validate();
//}
}

And the TextureButtons themselves are just JButtons with overridden paint() methods

Share this post


Link to post
Share on other sites
Solved it. Since the main gui is displaying the scrollpane and not the panel it contains, I had to do setPreferredSize() on the scrollpane rather than the panel that it contained silly me. Thanks for the replies :)

Share this post


Link to post
Share on other sites
I can't see anything that makes it bad, but I would design it this way instead - so it may be something in your design:

public TextureWindow(TileCache t) {
textureButtons = new ArrayList();
textureMap = new HashMap();
tileCache = t;
setPreferredSize(new Dimension(170, 400));
layout = new GridLayout(0, 4, 5, 5);
setLayout(layout);
addTex("grass1.gif");
scrolly = new JScrollPane(this);
}




It's kinda strange since I rougly the same design(although all my components(your texture button) are my own(img) ).

edit: too late - I need to use setPreferredSize since my main panel don't know how big it should be (must be why I didn't see that one ;) )

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!