Swapping components

Hi all!

I’ve got a simple multiplayer black jack game that has several “rooms” and chat. On the UI there’s a place for a Component that displays various room info while player is in lobby and a game board while he’s playing. I’ve got separate Components for this tasks. The question is: how to “swap” components in swing correctly?

I’ve tried calling remove, add and revalidate in sequence and that worked just fine on one of my laptops. On another one I’m having an exception saying that it’s impossible to create buffered strategy 'cause component’s dimensions are zeros. Unfortunately I don’t have the sources to attach right now. Maybe you could suggest some solution just by this symptoms?

Thanks in advance.

You need to use either CardLayout or JTabbedPane. CardLayout essentially swaps several panels in the same spot. JTabbedPane is basically a CardLayout panel with tabs to switch between the different panels.

Your problem with the buffer strategy is probably due to the window not being created yet. If it hasn’t been, getWidth() and getHeight() return zero. After you call setVisible() (and outside of the constructor), try creating your buffer strategy.

Thanks for your replies!

First of all, I’m using Table Layout (It’s not a part of core API, still very usefull). And window has been created already.

I couldn’t find out the true reason of this strange behavior but when I changed call sequence to


	remove();
	validate();
	repaint();

all worked fine. I’ve found the solution eventually.