Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Frames & Layout Manager

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all...
This is from RHE page 316
"It is given that the setSize() & setBounds() method on a component is overruled by Layout Manager. The major exception to this rule is the Frame class which is not under the thumb of Layout Manager & is perfectly willing to have u set the component size or bounds."
What i don't understand is that Frame class has Border Layout as default layout manager, Just look at below source code:-
public class FlowLay2 extends Frame
{
FlowLay2()
{
Button button = new Button("Click");
button.setBounds(70,80,40,40);
add(button);
button.addActionListener(this);
}

public static void main(String args[])
{
FlowLay2 f = new FlowLay2();
f.setBounds(150,100,300,200);
f.show();
}

Here i m trying to set the bounds of button, but it is placed in Center (Since Border Layout places the component at center by default).
So how can i setbounds for Button.
Thanks
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RHE just told you that Frame was an exception. They put in lot's of special code to MAKE it an exception. For all the other components their size is "overruled by the Layout Manager."
That means you DON'T setBounds().
What you CAN do is learn to work the containers to get the result that you want.
For instance, you can put a Panel in that center button area and make it GridLayout with several horizontal grids. Then you can put a panel in each of those grids. You can change the layoutManager of each of the Panels to whatever you want. By stacking and working the panels you can make just about any configuration that you want, including a panel that has just enough room to display the button that you want, the size that you want it.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
when u use setBounds(), it will not work cause a layout manager
will cover the effect .
in order to use setBounds succesfully , 1st set layout of the frame to null
setLayout(null);
then u can use setBounds which will work for sure.
hope this helps
regards
Kamal J
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think LONG and HARD before actually deciding to set LayoutManager to null. You are buying into doing about 300 layout management activities just to be able to setBounds(). I would certainly NOT recommend doing that.
reply
    Bookmark Topic Watch Topic
  • New Topic