• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to setPreferredSize of JPanel

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you cannot use setPreferredSize() when you use a BorderLayout.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is odd. How come they need to be new objects?
Thanks,
Barry
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Barry, button1's properties have already been set, so it wont change.
 
reply
    Bookmark Topic Watch Topic
  • New Topic