• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

LAYOUTS. PLS HLP !!!

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
the foll piece of code when run does not display any of the buttons.i was expecting button bc. however if the order of the layout managers is reversed,the code gives the standard output,ie buttons are displayed in flow layout.why is this so?
Ths in advance.
import java.awt.*;
public class layout
{
public static void main(String[] a)
{
Frame f = new Frame("Layout");
Button bn=new Button("North");
Button bs=new Button("South");
Button be=new Button("East");
Button bw=new Button("West");
Button bc=new Button("Center");
FlowLayout fl=new FlowLayout();
f.setLayout(fl);
f.add(bn);
f.add(bs);
f.add(be);
f.add(bw);
f.add(bc);
f.validate();
f.setLayout(new BorderLayout());
f.pack();
f.setVisible(true);
f.setSize(250,250);
}
}
Regards,
kaushik
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You changed your layout from flow to border and did not re-add the components using the new constraints.
David
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kaushik banerjee:
hi,
the foll piece of code when run does not display any of the buttons.i was expecting button bc. however if the order of the layout managers is reversed,the code gives the standard output,ie buttons are displayed in flow layout.why is this so
Ths in advance.
import java.awt.*;
public class layout
{
public static void main(String[] a)
{
Frame f = new Frame("Layout");
Button bn=new Button("North");
Button bs=new Button("South");
Button be=new Button("East");
Button bw=new Button("West");
Button bc=new Button("Center");
FlowLayout fl=new FlowLayout();
f.setLayout(fl);
f.add(bn);
f.add(bs);
f.add(be);
f.add(bw);
f.add(bc);
f.validate();
f.setLayout(new BorderLayout());
f.pack();
f.setVisible(true);
f.setSize(250,250);
}
}
Regards,
kaushik


Yah it will happen.For those Classes implementing Layoutmanager Interface retain the components(flow and grid etc) but those implementing LM2 Interface dont(i:e Border,Gridbag and Card etc).
I dont know why it happens??? Try to read api and find out whether there is any mention.
sandeep
[This message has been edited by sandeep bagati (edited April 13, 2001).]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Freels:
You changed your layout from flow to border and did not re-add the components using the new constraints.
David


Sooo, wht. !!!
Why this code is not showing any of the button ?
pls..someone explain.
thanks.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
LayoutMangers that implement the LayoutManger2 interface, such as BorderLayout, do not query the container for their components. They maintain their own list of components.
In the example, all the components were added to the container when the FlowLayout manager was in control. The BorderLayout is unaware of these components and hence does not display them.
If you reverse the layout assignments you'll see the components displayed as FlowLayout does query the container for it's components whenever a resize occurs.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Janes
can you elaborate about "Flowlayout does not query about its component when the container is resized".
Also if you change the layout manager between your programme how does it effect your previous add() statements?some code will be a great help!!
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry..About my previous post read it as"Flowlayout does query...
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am still not able to get how the layout's are behaving here look at the same code here
import java.awt.*;
public class me{
public static void main(String[] args){
Frame f=new Frame();
Button b1=new Button("one");
Button b2=new Button("two");
Button b3=new Button("three");
Button b4=new Button("four");
f.setLayout(new BorderLayout());
f.add(b1);
f.add(b2);
f.setLayout(new FlowLayout());
f.add(b3);
f.add(b4);
f.setSize(200,300);
f.setVisible(true);
}
}
Why 4 buttons are visible with FlowLayout manager.is it that changing the layout will put all the components with new settings.also flipping the layouts still give 4 buttons with flowlayout.i think here it does not honour the borderlayout's default settings ie placing the component at center.help please
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help please on Layout's...
 
kaushik banerjee
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI NACHIKET,
I THINK WHAT JANE MEANS IS THAT CARD,BORDER & GB MAINTAIN THEIR OWN LIST OF COMPONENTS FOR ARRANGING THE LAYOUT WHERE THEY ARE IN CHARGE WHERAS FLOW & GRID USE THE LIST MAINTAINED BY THE CONTAINER TO ARRANGE THE LAYOUT . SO THE FIRST 3 ARE UNABLE TO RECOGNISE ANY COMPONENTS THAT HAVE BEEN ADDED BEFORE SETTING THE LAYOUT TO THEM RESPECTIVELY
HOPE THIS HELPS.
REGARDS,
KAUSHIK
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Kaushik explained it
If you work through the API you'll see that the <code>Container.add( comp, constraints )</code> method states:


Adds the specified component to the end of this container. Also notifies the layout manager to add the component to this
container's layout using the specified constraints object.


The method notifies the component by calling the <code>addLayoutComponent()</code> method which is defined in the LayoutManger and LayoutManager2 interfaces.
Classes that implement LayoutManager eg FlowLayout and GridLayout, do not implement the <code>addLayoutComponent()</code> method.


Adds the specified component to the layout. Not used by this
class.


However, classes that implement LayoutManager2 eg CardLayout, BorderLayout, and GridBagLayout do


Adds the specified component to the layout, using the specified
constraint object. For border layouts, the constraint must be one of the following constants: NORTH, SOUTH, EAST, WEST, or CENTER.


When a container needs to redraw itself it calls the associated layout manager. If FlowLayout or GridLayout is active, they ask the container for a list of the components, since they don't retain their own list.
If GridBagLayout, CardLayout or BorderLayout is active, they layout the components based on their own list; they don't ask the Container how many components it has.
In the example code,

  • A container is created
  • It's layout manager is set to FlowLayout
  • Components are added
  • validate() is called, laying out the components
  • The layout manager is reset to BorderLayout
  • pack() is called, forcing the new layout to be displayed.
  • BorderLayout does not ask the Container for a list of it's components. In this case it's own internal list is empty as the components were added before it was assigned to the Container, so no components are displayed

  • Hope that helps.
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform
    [This message has been edited by Jane Griscti (edited April 15, 2001).]
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic