Java programming course: 13.3 Using inner classes

 In the previous lesson we looked at containers and JPanel.


Using Inner classes

Most classes you write are top-level classes, that is, independent classes in its own right and in their own class file. It is often useful to create inner classes, being classes that exist inside of another class. You would create an inner class when the outer class (i.e., the top-level class that contains it) requires a service that is specific to the outer class only. This is frequently the case when coding event listeners.

Inner classes are often declared private, though they don't have to be. You will now modify ExampleFrame to use an inner class as the listener for the centre button object, instead of the frame (the outer class) being the direct listener. 

Step 1

Surround the actionPerformed() method with the inner class declaration (this should be indented the same as if it were another method, and therefore the actionPerformed() method should be indented one level further in that it is currently):

private class ButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {
        // Get entered name
        String enteredName = nameField.getText();
        
        // Which button was clicked?
        Object source = event.getSource();
        if (source == myButton4) {
            // Convert name to uppercase
            System.out.println("Hello " + enteredName.toUpperCase());
        } else if (source == myButton5) {
            // Output the entered name as-is
            System.out.println("Hello " + enteredName);
        }
    }

}
 

Step 2

Remove the implements ActionListener section from the outer class declaration so that it reads as follows:

public class ExampleFrame extends JFrame { 

Step 3

Change the constructor to read as follows:

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 a ButtonListener (i.e inner class)
    ButtonListener buttonListener = new ButtonListener();
        
    // 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(buttonListener);
    panel.add(myButton4, BorderLayout.WEST);
        
    myButton5 = new JButton("Center");
    myButton5.addActionListener(buttonListener);
    panel.add(myButton5, BorderLayout.CENTER);
        
    // Add the panel to the frame
    add(panel); // defaults to BorderLayout.CENTER
        
    // Pack the components
    pack();
}
 

Verify that the frame correctly responds to the West and Centre buttons.


 In the next lesson we will define separate listeners for each event.

Next lesson: 13.4 Separate listeners for each event


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Monday, 27 October 2025

Captcha Image