• 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

AWT

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below shows a blank frame instead of showing a single button named "Center" occupying the whole of the frame. Could anyone please explain?
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);
setLayout(new BorderLayout());
setSize(300,300);
setVisible(true);
validate();
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

Thanx
Uvnik
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code adds all the comps to a flow layout.
then creates a new border layout. when you setVisible
to true, this acts on the border layout to which no
comps are added.
IMO, the other way may work okay ie; first border then
flow. I think border layout does something special.
If I am not wrong, this was discussed before. If no one
replies, you could try the search option...just suggesting.
regds.
- satya
 
Uvnik Gupta
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Satya. I could find a thread that discussed this issue. But am still not clear why doesn't it work only for BorderLayout. It works for all other layouts without changing the position of setVisible().
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

If you are using FlowLayout and GridLayout to set the layout then you can set it after adding components.
But , if you are using BorderLayout to set the layout then u must need to set it before adding the components.
thanks
sdev
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe this has something to do with constraints. When you add components to a container which is using the LayoutManager2 interface i.e. BorderLayout, GridBagLayout, CardLayout, etc you have to explicitly specify the constraints object which will tell the layout manager where to position the componets inside the container. For eg:
add(button, BorderLayout.SOUTH);
However if you use FlowLayout or GridLayout which implement the LayoutManager interface, you do not have to set the constraints object. Infact you do not even have control over where the components will be placed. The LayoutManager takes care of that. I guess that's why it's behaving like this.
Have you tried setting it to GridLayout instead of BorderLayout? How does it behave then?
 
Uvnik Gupta
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junaid,
I tried all (Grid, GridBag, Card, Flow and Border). But it does not work only with BorderLayout.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My opinion is that this has to do with the default settings for the layout managers. When you create a LayoutManager without giving any constraints, the default constraints will be used. If you add a component to the BorderLayout without giving any constraints, for example, it will be added to the center pane, and componets added later will simply cover the ones added before so you only see the last one added. The following code:
import java.awt.*;
public class test 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 test()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
setSize(300,300);
setVisible(true);
validate();
}
public static void main(String args[])
{
test tf = new test();
}
}
is equivalent to:
import java.awt.*;
public class test 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 test()
{
setLayout(new BorderLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setSize(300,300);
setVisible(true);
validate();
}
public static void main(String args[])
{
test tf = new test();
}
}
and I guess the same should be ture for other layout managers
 
Uvnik Gupta
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Benli,
The point is that the two snippets posted by you are not equivalent Compile and see the difference.
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uvnik,
You're right. It works even for GridBagLayout & CardLayout. BorderLayout is the only exception. I wonder why
 
This is my favorite 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