File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abhilash Quiz - Question on Layout

 
Junaid Bhatra
Ranch Hand
Posts: 213
  • 0
  • 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?

 
Khalid Bou-Rabee
Ranch Hand
Posts: 54
  • 0
  • 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
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • 0
  • 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
 
rajsim
Ranch Hand
Posts: 116
  • 0
  • 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?
 
Sandra Marti
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has been discussed here
 
sasank manohar
Ranch Hand
Posts: 186
  • 0
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic