Event Handling


Goals

Event Classes


Frame Windows

A Frame with Two Labels


File FrameTest.java

1 import javax.swing.ImageIcon;
2 import javax.swing.JFrame;
3 import javax.swing.JLabel;
4 import javax.swing.JPanel;
5
6 /**
7    This program displays a frame with an image and a text label.
8 */
9 public class FrameTest
10 {  
11    public static void main(String[] args)
12    {  
13       JFrame frame = new JFrame();
14       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15
16       JLabel iconLabel = new JLabel(new ImageIcon("world.gif"));
17       JLabel textLabel = new JLabel("Hello, World!");
18
19       JPanel panel = new JPanel();
20       panel.add(iconLabel);
21       panel.add(textLabel);
22       frame.setContentPane(panel);
23
24       frame.pack();
25       frame.show();
26    }
27 }

Text Components

A Text Area with Scroll Bars


The Control Panel for Adding Interest


File TextAreaTest.java


1 import java.awt.event.ActionEvent;
2 import java.awt.event.ActionListener;
3 import javax.swing.JButton;
4 import javax.swing.JFrame;
5 import javax.swing.JLabel;
6 import javax.swing.JPanel;
7 import javax.swing.JScrollPane;
8 import javax.swing.JTextArea;
9 import javax.swing.JTextField;
10
11 /**
12    This program shows a frame with a text area that displays
13    the growth of an investment. A second frame holds a text
14    field to specify the interest rate.
15 */
16 public class TextAreaTest
17 {  
18    public static void main(String[] args)
19    {  
20       // the application adds interest to this bank account
21       final BankAccount account = new BankAccount(INITIAL_BALANCE);
22       // the text area for displaying the results
23       final JTextArea textArea = new JTextArea(10, 30);
24       textArea.setEditable(false);
25       JScrollPane scrollPane = new JScrollPane(textArea);
26
27       // construct the frame for displaying the text area
28       JFrame frame = new JFrame();
29       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
30       frame.setContentPane(scrollPane);
31       frame.pack();
32       frame.show();
33
34       // the label and text field for entering the interest rate
35       JLabel rateLabel = new JLabel("Interest Rate: ");
36
37       final JTextField rateField = new JTextField(10);
38       rateField.setText("" + DEFAULT_RATE);
39
40       // the button to trigger the calculation
41       JButton calculateButton = new JButton("Add Interest");
42       
43       class CalculateListener implements ActionListener
44       {
45          public void actionPerformed(ActionEvent event)
46          {
47             double rate = Double.parseDouble(
48                rateField.getText());
49             double interest = account.getBalance() 
50                * rate / 100;
51             account.deposit(interest);
52             textArea.append(account.getBalance() + "\n");
53          }            
54       }
55
56       ActionListener listener = new CalculateListener();
57       calculateButton.addActionListener(listener);
58
59       // the control panel that holds the input components
60       JPanel controlPanel = new JPanel();
61       controlPanel.add(rateLabel);
62       controlPanel.add(rateField);
63       controlPanel.add(calculateButton);
64
65       // the frame to hold the control panel
66       JFrame controlFrame = new JFrame();
67       controlFrame.setContentPane(controlPanel);
68       controlFrame.pack();
69       controlFrame.show();
70    }
71
72    private static final double DEFAULT_RATE = 10;
73    private static final double INITIAL_BALANCE = 1000;
74 }

The MouseListener Interface

public interface MouseListener
{
void mousePressed(MouseEvent event);
//Called when a mouse button has been pressed on a component
  void mouseReleased(MouseEvent event);
  //Called when a mouse button has been released on a component
  void mouseClicked(MouseEvent event);
//Called when the mouse has been clicked on a component
  void mouseEntered(MouseEvent event);
//Called when the mouse enters a component
void mouseExited(MouseEvent event);
//Called when the mouse exits a component
}

File SpyMouseListener.java

1 import java.awt.event.MouseEvent;
2 import java.awt.event.MouseListener;
3
4 /**
5    This listener simply prints out the listener method name
6    and the x- and y-coordinate of the mouse position.
7 */
8 public class SpyMouseListener implements MouseListener
9 {  
10    public void mousePressed(MouseEvent event)
11    {  
12       System.out.println("Mouse pressed. x = " 
13          + event.getX() + " y = " + event.getY());
14    }
15
16    public void mouseReleased(MouseEvent event)
17    {  
18       System.out.println("Mouse released. x = " 
19          + event.getX() + " y = " + event.getY());
20    }
21
22    public void mouseClicked(MouseEvent event)
23    {  
24       System.out.println("Mouse clicked. x = " 
25          + event.getX() + " y = " + event.getY());
26    }
27
28    public void mouseEntered(MouseEvent event)
29    {  
30       System.out.println("Mouse entered. x = " 
31          + event.getX() + " y = " + event.getY());
32    }
33
34    public void mouseExited(MouseEvent event)
35    {  
36       System.out.println("Mouse exited. x = " 
37          + event.getX() + " y = " + event.getY());
38    }
39 }

To be put in, e.g., the Panel class

11       SpyMouseListener listener = new SpyMouseListener();
12       addMouseListener(listener);


Spying on Mouse Events


Nutshell:
How to get a panel to respond to clicks


Often, the following ideas are useful:

class ImagePanel {
    .....
    void moveTile(...) { ... }
    .....
    addMouseListener(new MouseAdapter() {
       public mouseClicked(...) {
          ...
          }
    });
}