views:

135

answers:

1

I am using swing to create my GUI. J have a JFrame containing one main JPanel which, in its turn contain several JPanels which, in their turn, contain buttons.

I would like to set certain sizes to mu buttons and JPanels. How does it work? Should I set sizes of my buttons and then the size of the JPanel and the JFrame will be set according to the buttons sizes? Or it works in the opposite direction? I set size for the JPanel and the size of the buttons will be set automatically?

ADDED:

I used fieldPanel.setPreferredSize(new Dimension(100,100)); and it had no effect on the appearance of the window. Can it be because this panel is in the "middle of the hierarchy" it contains some buttons and it is contained by other JPanels?

+3  A: 

It depends on the layout manager that you use. Some, like GridLayout does not pay attention to the size that you set but rather apply the rules of the layout manager itself.

Others, like GridBagLayout, you have better control over the size of the components.

Vincent Ramdhanie
Yes, I have the GridLayout. So, now I know why it did not work. In the grid I have buttons. May be I can set their size explicitly and, in this way, set the size of the JPanel?
Roman
If the size of the buttons are important then I recommend using another layout manager. Or you can put the button in a JPanel and put the JPanel in the GridLayout container. Then, the JPanel in which the button reside may be given a flow layout which does not affect the size of the Button so you would get the button sized to your preferred size. However, this is a clumsy approach which would not give you a lot of control over teh layout of your GUI.
Vincent Ramdhanie