File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to setPreferredSize of JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to setPreferredSize of JPanel" Watch "How to setPreferredSize of JPanel" New topic
Author

How to setPreferredSize of JPanel

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi All,
I figured the swing folks could help me out with this one. I am having some confusion about how the setPrefferedSize() method works, or does it even work on JPanels? I want 2 panels, a north panel and a south panel, and I want each of them to share the JFrame equally. I have set their preffered size to be the same. When I run this sample code below, I only see the south panel. If I maximize the window, I can see the north panel, but only briefly. But, I can definitely tell that it is not in the right place. Can someone please tell me why this is happening? I am sure this is something simple, but I cannot figure out what.
Many thanks to you!!!

Barry
marilyn murphy
Ranch Hand

Joined: Aug 28, 2001
Posts: 84
I think that you cannot use setPreferredSize() when you use a BorderLayout.
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
You can use setPreferredSize() in BorderLayout also. The problem lies in the code, you are adding the same component to the other Panel & thats why you can't see the components in the North Panel. Use new Objects then u can see the components in the North Panel also.


- Vinod<br />-------<br />SCJP2
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

That is odd. How come they need to be new objects?
Thanks,
Barry
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Barry,
The reason is that each GUI component (AWT or Swing) keeps track of its own parent, bounds, etc. Knowing this we can say that an object can only be in one place at one time.
The other reason is basic Java. Method parameters are passed by value in Java. What does that mean? It means that when we pass the value 'button1' into the add method we are actually receiving a copy of the reference to button1. We are still pointing to the object button1. Therefore, using the above reasoning we can see why the button1 state (parent, bounds, etc.) is changed inside the add method. Using the same or another reference to the same object will again change the button1 state. Therefore, a single object can only be in one state (in one container) at a single time.
Long winded, but maybe effective?
Manfred.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Ah yes! This does make sense. Thanks a lot!

Barry
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Another question along the same lines. If I had 2 buttons and I wanted to set the preferred size for each button to be different, would I need to create 2 Dimension objects or could I use the same one and just setSize() to something else? So something like this:
Dimension d = new Dimension(50,30);
JButton button1 = new JButton("Button1");
JButton button2 = new JButton("Button2");
button1.setPrefferedSize(d);
d.setSize(60,40);
button2.setPrefferedSize(d);
By changing the state of "d", I won't be also changing the state of button1. Is this correct?

Thanks,
Barry
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
You are right Barry, button1's properties have already been set, so it wont change.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to setPreferredSize of JPanel