Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Frame

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
//HERE
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}

Can someone tell me why this will compile and run cleanly, but no component is visible?
If i move setVisible(true); to the line commented //HERE it will show five buttons in a order which doesnt look like flow or border layout.
 
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 Deepali,
BorderLayout requires adds using constraints. No constraints are specified in your case therefore no components will be shown.
By moving the setVisible method you will indeed get a flowLayout (i.e., all buttons placed one after the other from left to right maybe taking up more than one row depending on width of frame).
Regards,
Manfred.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Leonhardt:
Hi Deepali,
BorderLayout requires adds using constraints. No constraints are specified in your case therefore no components will be shown.
By moving the setVisible method you will indeed get a flowLayout (i.e., all buttons placed one after the other from left to right maybe taking up more than one row depending on width of frame).
Regards,
Manfred.


If we dont specify any constraints for Borderlayout then by default it should place in center so i guess w should get the last button placed in teh center.
If i move setVisible at //HERE if it was following cflowlayout i should had them in a row but it is not so.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepali, i�m not sure why this happens, but it must be something specific with BorderLayout behaviour. Since you didn�t define any constraints, all the buttons should be placed at CENTER, for some reason, instead of making the last button use all of the window space, the Layout Manager puts all buttons in position(0,0) of the frame and width=0 and height=0. That�s why we couldn�t see anything! they were all "hidden" in the top-left corner. Run this code and you�ll see what I mean:

It will show the position and size of each button in the pretty FlowLayout.Like this:


Output:
North
Size: width=44, height=23
Location: x=29, y=28
South
Size: width=46, height=23
Location: x=78, y=28
West
Size: width=42, height=23
Location: x=129, y=28
East
Size: width=39, height=23
Location: x=176, y=28
Center
Size: width=51, height=23
Location: x=220, y=28


If you uncomment that setLayout() method, everything will be zero. If any bartender or anyone knows why this happens, please help me.
Francisco
[ June 28, 2002: Message edited by: Francisco A Guimaraes ]
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepali Pate:

If i move setVisible at //HERE if it was following cflowlayout i should had them in a row but it is not so.


Thats coz the buttons ocupy the frame according to the size of the frame ......by moving the setVisible at //HERE , the buttons are set in a flowlayout according to the size of the frame by default.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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(bSouth,"South"); //1
add(bWest,"West"); //2
add(bEast,"East"); //3
add(bCenter,"Center");//4
add(bNorth,"North"); //5
//Here
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
}

public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
I modified the question and my question is
I move setVisible(true) to //Here
Y do i see only "North" there .... irrespective of the order of stmts 1,2,3,4,5?
 
nupur dhawan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you move setVisible(true) to //HERE....it means that the frame has a layout manager set to Border Layout .....therefore it displays the last button ..in this case the one on //5 ..i.e "North".
If you were to leave the code as it is , it displays the buttons using the FlowLayout as that statement resets the LayoutManager and is set to be visible after it has been reset .
I hope this clarifies your query .
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i move setVisible //HERE all the buttons are displyed but they donts seem to follow flowlayout or borderlayout.
I get two buttons in first row, two in second and one in last.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic