This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to setPreferredSize of JPanel

 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you cannot use setPreferredSize() when you use a BorderLayout.
 
Vinod Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is odd. How come they need to be new objects?
Thanks,
Barry
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes! This does make sense. Thanks a lot!

Barry
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Barry, button1's properties have already been set, so it wont change.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic