GridLayout :: help needed
hello every1
i need a small help regarding grid layout ... i've created a grid layout of 3 rows & 3 cols, every cell contains a button and every button is associated with an action listener ... now whenever any button is pressend, iz there any way to get the coordinates of that button, i mean if i press button "6" ... then on the command prompt, message should be displayed "row:2 col:3" ... similarly, i want 2 do the same operation on every button. so iz it possible to do this ???
below i m pasting my code, i just need the coding for actionPerformed() method ... i hope that i'll get good reply as soon as possible
CHEERS !!!
import java.awt.*;
import java.awt.event.*;
class GridDemo extends Frame implements ActionListener
{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
public GridDemo()
{
b1=new Button("1");
b1.addActionListener(this);
b2=new Button("2");
b2.addActionListener(this);
b3=new Button("3");
b3.addActionListener(this);
b4=new Button("4");
b4.addActionListener(this);
b5=new Button("5");
b5.addActionListener(this);
b6=new Button("6");
b6.addActionListener(this);
b7=new Button("7");
b7.addActionListener(this);
b8=new Button("8");
b8.addActionListener(this);
b9=new Button("9");
b9.addActionListener(this);
setLayout(new GridLayout(3,3));
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
}
public void actionPerformed(ActionEvent ae)
{
// WHAT TO DO HERE ???
}
public static void main(String args[])
{
GridDemo gd=new GridDemo();
gd.setSize(300,300);
gd.setVisible(true);
}
}
|