Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] More Swing hassles - scrollbars

Started by Vyper_uk Aug 9, 2005 at 12:11 PM 5 replies 1k views
Original Post
Vyper_uk
Vyper_uk
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
sirGustav
sirGustav
knowing absolutely nothing of your internal functions, this looks like it could cause the trouble:
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
Vyper_uk
Vyper_uk
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
sirGustav
sirGustav
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?
Vyper_uk
Vyper_uk
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
Vyper_uk
Vyper_uk
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 :)
sirGustav
sirGustav
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 ;) )

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.