Author Topic:   applets
Prasad
unregistered
posted February 02, 2000 04:06 AM           
import java.applet.*;
import java.awt.*;

public class Q20 extends Applet
{
Button okButton = new Button("Ok");
public void init() {
setLayout(new BorderLayout());
add("South", okButton);
add("North", okButton);
add("East", okButton);
add("West", okButton);
add("Center", okButton);

}
}

please tell me as to why the above applet behaves the way it does. I expect it to display five buttons positioned in five respective places. But only the 'Center' button is being displayed.

prasad

Tony Alicea
sheriff
posted February 02, 2000 09:35 AM             
The correct way is add(Component c, Object Constraints)

That translates to

add(okButton, "North")

although I prefer

add(okButton, BorderLayout.NORTH)

maha anna
bartender
posted February 02, 2000 01:18 PM             
Hi Tony!
I was testing prasad's program and I came to know the foll.
The problem is not the syntax. Both add("North",bn) and
add(bn,GridBagLayout.NORTH) are correct
. This problem happens ONLY WHEN WE ADD THE SAME OBJECT again and again. It does not happen when we add diff objects it works fine. I created 5 diff btns and added them and it worked fine also.
The same this happens when we change the Layout to FlowLayout also. It appears as though when we add the same object again the layout manager just clears the old place... For simplicity I changed this as an application. The same thing happens in applet also.
Do you have any idea why this happens....
code:

import java.awt.*;
import java.io.*;

public class mframe extends Frame{

Button bn,bs,be,bw,bc;
public mframe(){
super("maha");

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");
/*
setLayout(new FlowLayout());
add(bn);
add(bn);
add(bn);
add(bn);
add(bn);
*/

try{
add("North",bn);// If we comment out this line be
// & bw resize vertically ok.
setSize(300,300);
setVisible(true);
System.out.println("Added West and sleepling");
Thread.sleep(2000);

add("East",be);
setSize(300,300);
setVisible(true);
System.out.println("Added East and sleepling");
Thread.sleep(2000);

add("South",bn);
setSize(300,300);
setVisible(true);
System.out.println("Added South and sleepling");
Thread.sleep(2000);

add("West",bw);
setSize(300,300);
setVisible(true);
System.out.println("Added West and sleepling");
Thread.sleep(2000);

add("Center",bc);
setSize(300,300);
setVisible(true);
System.out.println("Added Center and sleepling");
Thread.sleep(2000);

}catch(InterruptedException e) {}

}
public static void main(String args[]){

new mframe();
}
}


[I split one overlong line in the code - Jim]

[This message has been edited by Jim Yingst (edited February 02, 2000).]

Paul Wheaton
sheriff
posted February 02, 2000 06:22 PM             
The layout manager keeps track of all the objects added. Later you can call remove( component ) and it will remove the one component that you reference. So if you add the same component, it isn't really added, it sort of replaces the previous instance of the component.

|