JPanel size problem

N

Neutrino

Hello,
I'm trying to specify a size for a JPanel component inside a JFrame. I use
the methos setSize, but this is not working, I cannot find the reason, could
you help me to find my mistake ?
Thanks a lot,
AR

.....
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setTitle ("Statistics");
setSize (600,600); // --> IS WORKING

Container contentPane = getContentPane();

JPanel win = new JPanel ();
win.setLayout(new BoxLayout(win, BoxLayout.Y_AXIS));
win.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JPanel up = new JPanel ();
up.setBorder (BorderFactory.createTitledBorder ("Statistics"));
up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));
up.setAlignmentX(Component.CENTER_ALIGNMENT);
up.setSize (180, 100); // --> NOT WORKING

// There is here some JLabel....

win.add (up);
contentPane.add (win);
//pack ();
setVisible (true);
....
 
A

andreas kinell

Hello,
I'm trying to specify a size for a JPanel component inside a JFrame. I use
the methos setSize, but this is not working, I cannot find the reason,
could
you help me to find my mistake ?
Thanks a lot,
AR

....
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setTitle ("Statistics");
setSize (600,600); // --> IS WORKING

Container contentPane = getContentPane();

JPanel win = new JPanel ();
win.setLayout(new BoxLayout(win, BoxLayout.Y_AXIS));
win.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JPanel up = new JPanel ();
up.setBorder (BorderFactory.createTitledBorder ("Statistics"));
up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));
up.setAlignmentX(Component.CENTER_ALIGNMENT);
up.setSize (180, 100); // --> NOT WORKING

// There is here some JLabel....

win.add (up);
contentPane.add (win);
//pack ();
setVisible (true);
...

usually, setPreferredSize(x,y).

(belongs to comp.lang.java.gui)

andreas
 
B

Babu Kalakrishnan

Neutrino said:
I'm trying to specify a size for a JPanel component inside a JFrame. I use
the methos setSize, but this is not working, I cannot find the reason, could
you help me to find my mistake ?

....
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setTitle ("Statistics");
setSize (600,600); // --> IS WORKING

Container contentPane = getContentPane();

JPanel win = new JPanel ();
win.setLayout(new BoxLayout(win, BoxLayout.Y_AXIS));
win.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JPanel up = new JPanel ();
up.setBorder (BorderFactory.createTitledBorder ("Statistics"));
up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));
up.setAlignmentX(Component.CENTER_ALIGNMENT);
up.setSize (180, 100); // --> NOT WORKING

// There is here some JLabel....

win.add (up);
contentPane.add (win);
//pack ();
setVisible (true);
...

Setsize() "works" only when called on Components that are not under the
control of any LayoutManager. (The reason why it worked for the JFrame,
but nor for the JPanel in your code).

I put quote marks around the word "works" because strictly speaking the
above statement is incorrect. What actually happens is that the
Component does change its size to the value you specify, but when the
GUI validation occurs, the LayoutManager of the parent container sets it
to another size based on its Layout strategy, and so the effect of your
setSize call gets overwritten by the Layoutmanager's setSize call.

The suggestion by the other poster "use setPreferredSize()" is very
subjective as well. If your JPanel contains other components, it is best
to *not* call setPreferredSize() on it, this allows it to compute its
preferred size based on the preferred size of its contents and the
Layoutmanager applied to it. On the other hand if your JPanel has no way
of determining its preferred size (say it is just a painting surface),
you can use the setPreferredSize call to set its preferred size. (Even
if you do this, the ultimale size the panel ends up with will depend
upon the LayoutManager employed by the parent container and the
constraints you specified while adding it to the parent).

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top