• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Abhilash Quiz - Question on Layout

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question is from Abhilash's quiz, Question 39.

The above code compiles and runs cleanly. However none of the buttons are displayed on the screen. Why is this happening?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to be a conflict between LayoutManager and LayoutManager2...
It's a little late, need coffee... :|
Khalid
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think we need help of java experts.
Hellooooooooo??

Any help would be appreciated here.
vivek
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It must be because BorderLayout uses constraints like north, south and FlowLayout doesn't use any.
What do you think be displayed if you set the layout to GridBagLayout?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has been discussed here
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
import java.awt.*;
public class TestFrame extends Frame
{
Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
setLayout(new FlowLayout());
//setLayout(new GridLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new FlowLayout());
//setLayout(new GridLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

For FlowLayout and GridLayout you can set the layout after adding the components using add() method. I mean ,It doesn't matter , if you set the layout before or after adding components.
But for BorderLayout, the constraint must be one of the following strings: "North", "South", "East", "West", or "Center".
And you must set the layout before adding the components, this is also not necessary if you are using for the container Frame. As u know BorderLayout is the default Layout manager for Frame.

import java.awt.*;
public class TestFrame extends Frame
{
Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
//setLayout(new BorderLayout());
add(bNorth,BorderLayout.NORTH);
add(bSouth,BorderLayout.SOUTH);
add(bWest,BorderLayout.CENTER);
add(bEast,BorderLayout.EAST);
add(bCenter,BorderLayout.WEST);
setLayout(new BorderLayout());

setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

If you have set the layout as BorderLayout or used the container Frame . And you are setting the layout before adding the components. While adding the components you must mention the constraints of NORTH, SOUTH ....etc., . Or if you donot mention the constraints then you can see only the last component occupying the whole frame. That is because it overlaps all other components , if you don't mention the constraints..



import java.awt.*;
public class TestFrame extends Frame
{
Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
setLayout(new BorderLayout());

add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
In this program only the Button "Center" will display occupying the whole frame and overlaping all other buttons.
Thanks
sdev
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic