tags:

views:

28

answers:

2

Ok, so I've been pulling my hair out over this all day. I cannot get my panels to resize. I add a panel to anything.. a frame, a content pane, and It wont .. "force" setBounds for some strange reason.

Container pane = window.getContentPane();
JPanel calendarPanel = new JPanel(); 
pane.add(calendarPanel); 
calendarPanel.setBounds(0,0, 320, 335); 

note that window is of JFrame. Any suggestions to sizing a panel would be appreciated!

+1  A: 

If you haven't already, it sounds like you need to look at layout managers. Some layout managers honour the requested or preferred sizes of components, others don't.

By default a JFrame uses BorderLayout as the layout manager. When you add your calendar panel above without supplying constraints, it assumes you want the panel centred. Anything centred in a BorderLayout takes up all the available space, no matter what you set the bounds of the component to.

For an immediate fix to your problem, set the layout manager to null:

Container pane = window.getContentPane();
pane.setLayout( null );
Ash
A: 

Did you set the Layout of the parent component? I think the FlowLayout leaves the panel to his natural size.

component.setLayout(new java.awt.FlowLayout());

EDIT: see Ash's post

SK.