In the previous lesson we saw how to define inner classes.
Separate listeners for each event
As programs become more complex it can be helpful to define a separate listener class for each individual event rather than use just one listener to serve all.
Step 1
Rename ButtonListener to CenterButtonListener. The most effective way to do this is by right-clicking on the ButtonListener text on the inner class declaration and selecting Refactor | Rename. Then enter CenterButtonHandler in the New Name box and click Refactor. As well as renaming the inner class, NetBeans will automatically locate and find everywhere it is referenced and update those as well.
Step 2
Simplify the actionPerformed() method inside CenterButtonHandler to just output the text entered into nameField:
private class CenterButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello " + nameField.getText());
}
}
Step 3
Modify the call to addActionListener() against myButton5 to instantiate a new CenterButtonHandler object instead of specifying buttonListener:
myButton5.addActionListener(new CenterButtonHandler());
Step 4
At the end of the source code enter another inner class to handle the west button:
private class WestButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello " +
nameField.getText().toUpperCase());
}
}
Step 5
Modify the call to addActionListener() against myButton4 to instantiate a new WestButtonHandler object instead of specifying this:
myButton4.addActionListener(new WestButtonHandler());
Step 6
Remove the following statements from the constructor as they are no longer needed:
// Create a ButtonListener (i.e inner class) ButtonListener buttonListener = new ButtonListener();
Step 7
Verify that the application works as before.
The complete source code for ExampleFrame should look as follows:
package virtualzoo.ui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ExampleFrame extends JFrame {
private JTextField nameField;
private JButton myButton2, myButton3, myButton4, myButton5;
public ExampleFrame() {
super("My Application");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLayout(new BorderLayout());
// Create a panel to hold the components
JPanel panel = new JPanel(new BorderLayout());
// Create the components and add to the panel
JPanel nameEntryPanel = new JPanel();
JLabel nameLabel = new JLabel("Enter your name:");
nameField = new JTextField(15);
nameEntryPanel.add(nameLabel);
nameEntryPanel.add(nameField);
panel.add(nameEntryPanel, BorderLayout.NORTH);
myButton2 = new JButton("South");
panel.add(myButton2, BorderLayout.SOUTH);
myButton3 = new JButton("East");
panel.add(myButton3, BorderLayout.EAST);
myButton4 = new JButton("West");
myButton4.addActionListener(new WestButtonHandler());
panel.add(myButton4, BorderLayout.WEST);
myButton5 = new JButton("Center");
myButton5.addActionListener(new CenterButtonHandler());
panel.add(myButton5, BorderLayout.CENTER);
// Add the panel to the frame
add(panel); // defaults to BorderLayout.CENTER
// Pack the components
pack();
}
private class CenterButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello " + nameField.getText());
}
}
private class WestButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello " + ameField.getText().toUpperCase());
}
}
}
To handle the other buttons, you can follow the same pattern of defining an inner class and instantiating an object of that type to serve as the button's listener.
In the next series of lessons we will look at common GUI components.
Comments