In Swing/AWT you have a multiple options when dealing with events. Here are a few examples.
1. You can implement a listener anonymously.
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null , "Clicked!");
}
});
This can be very nice when you just want to relay to another method. This option often leaves logic right in the GUI code.
2. You can create a new named class to handle events.
public class MyEventHandler implements MouseListener
{
private View view;
public MyEventHandler(View view)
{
this.view = view;
}
public void mouseClicked(MouseEvent e)
{
System.out.println("Clicked");
JButton button = (JButton)e.getSource();
if(button == view.startButton)
{
start();
}
else if(button == view.stop)
{
stop();
}
else
{
restart();
}
}
public void mousePressed(MouseEvent e)
{
System.out.println("Pressed");
}
public void mouseReleased(MouseEvent e)
{
System.out.println("Released");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("Entered");
}
public void mouseExited(MouseEvent e)
{
System.out.println("Exited");
}
// add other methods to assist event handling //
}
This class can be used to handle events for multiple components.