Hi all,
I'm a beginner in JAVA studying SWING. I learned that if we want to handle an event for a event source(say JButton) we should implement corresponding Event Listener(say ActionListener) .
I also understood that we should register the Listener with that event source.
With that knowledge I tried this program
import javax.swing.*;
import java.awt.event.*;
public class SimpleGui2 implements MouseListener,ActionListener {
SimpleGui2 insGui;
JButton button= new JButton("Click me");
public static void main(String[] args) {
SimpleGui2 gui = new SimpleGui2();
gui.start();
}
public void start() {
JFrame frame = new JFrame();
//button.addMouseListener(this);
//button.addActionListener(this);
button.addActionListener(insGui);
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
button.setText("Triggered");
}
public void mouseClicked(MouseEvent me) {
button.setText("I have been clicked");
}
public void mouseEntered(MouseEvent me) { }
public void mouseExited(MouseEvent me) { }
public void mousePressed(MouseEvent me) { }
public void mouseReleased(MouseEvent me) { }
}
but It's is not working as I expected.
or precisely why when we clicked the actionPerformed/mouseClicked method is not invoked ?
if i change the same with argument "this" it's working .
I guess "this" refers to current object (which is also of type SimpleGui2 which implemented ActionListener)
Can any one please clarify me what happens behind the scenes ?
Thanks in advance