Making a square JPanel [Solved]

Hi there !
It's been a long time since I hadn't posted here, but here I am with a new question :D
I have a JFrame which supposedly contains a map editor. No problem about that though, it's just that I would like to have two JPanels as components of my frame's ContentPane. One to have the actual map (in working progress) and another one to have all the different tiles, options, etc. But, as my map is always square-shaped, I would like to have one of these JPanels to always be a square. Something looking like this :

http://img11.hostingpics.net/pics/178590schema.png

The code for this would be something like :
optionPanel.setBounds(0,0,frame.getWidth()-frame.getHeight(),frame.getHeight());
mapPanel.setBounds(frame.getWidth()-frame.getHeight(),0,frame.getWidth()-frame.getHeight(),frame.getHeight());
I tried using this piece of code but it seems like it won't update when I resize the frame. I also tried using a GridBagLayout but I don't seem to find how to do such a thing. There must be something incredibly simple I haven't thought of, but isn't it the aim of this newbie section to help those like me ? ;D
Anyway, thanks a lot to anyone answering me :)
J0

Have you tried [icode].setPreferredSize(Dimension dimension)[/icode]? It’s been a while since I’ve worked with swing components but I think that should work.

CopyableCougar4

[quote=“CopyableCougar4,post:2,topic:50255”]
Well I’ll be trying it in a few seconds, thanks for the answer :slight_smile:
J0

You are setting your panel size to a fixed size (i.e. current width - current height). So you have to set the dimensions everytime the jframe resizes

Same as what I tried first, [icode].setPreferredSize[/icode] works at first but won't update whenever I resize my JFrame... Too bad. :-\

[quote=“Kefwar,post:4,topic:50255”]
Yeah I understood this at some point, but I don’t know how to update these variables each time some resizing is done… Some solution here please ? :persecutioncomplex:
Thanks anyway :wink:
J0

Try something like this: http://stackoverflow.com/a/8917978/3517265

You can either set the frame resizable to false

frame.setResizable(false);

Or you can add a component listener to the frame and put your coding inside the method “componentResized”, like so:


frame.addComponentListener(new ComponentAdapter() {
	public void componentResized(ComponentEvent e) {
		// put your code to resizing the frame here
	}
});
Thanks a lot to all of you ! I'll try your solutions and I'm sure at least some will work ! ;D
Well, I'll try every solution but [icode]frame.setResizable[/icode] because that's not what I'm looking for :P But you couldn't know, could you ? So thanks as well ! ::)
J0