[SOLVED] ActionEvent.setSource() is failing in Swing while working in AWT
hello,
I have a class which works under AWT but fails under swing.
The part fails under swing uses ActionEvent.setSource(..)....
For ex. following code works...
Code:
public class Pad extends Panel implements ActionListener
{
private Button ok = new Button("OK");
public Pad()
{
setName("MyPad");
setLayout(new FlowLayout());
add(ok);
ok.addActionListener(this);
}
public void addActionListener(ActionListener listnr)
{
ok.addActionListener(listnr);
}
public void actionPerformed(ActionEvent e)
{
e.setSource(this);
}
}
Now the same code fails if I make it swing...
Code:
public class JPad extends JPanel implements ActionListener
{
private JButton ok = new JButton("OK");
public JPad()
{
setName("MyJPad");
setLayout(new FlowLayout());
add(ok);
ok.addActionListener(this);
}
public void addActionListener(ActionListener listnr)
{
ok.addActionListener(listnr);
}
public void actionPerformed(ActionEvent e)
{// [COLOR="Red"]Here is the problem I think[/COLOR]
e.setSource(this);
}
}
Bellow is a test code to clarify the problem...
Code:
public class JPUser extends JFrame implements ActionListener
{
private JButton ok = new JButton("OK");
private JPad swPad = new JPad();
private Pad awtPad = new Pad();
public JPUser()
{
super("Test");
setLayout(new FlowLayout());
add(ok);
add(swPad);
add(awtPad);
ok.addActionListener(this);
swPad.addActionListener(this);
awtPad.addActionListener(this);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//System.out.println(e.getSource());
if(e.getSource() == swPad)
System.out.println("Swing Pad -- OK"); //[COLOR="Red"] I never see this line[/COLOR]
else if(e.getSource() == awtPad)
System.out.println("AWT Pad -- OK");
else
System.out.println("OK");
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new JPUser();
}
});
}
}
Please can anyone help...