In Java, it is possible to write a class definition inside of another class definition. The inside class is called an inner class.
A class with an inner class
1 2 3 4 5 6 |
Public class Outerclass{ //fields and methods of the outer class appear here Private class InnerClass{ //fields and methods of the inner class appear here } } |
The inner class is usually made private. When an inner class is private, it is accessible only to the code in the class that contains it. The inner class is a private member of the outer class. The inner class, like other methods of the outer class, is accessible to only methods of the outer class. The class is defined inside the outer class because it is needed right in the outer class.
Inner classes are commonly used to write event listener classes to handle events from GUI components.
Program to demonstrate private inner class as event listener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package villagecoder; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /* * This program demonstrates event handling of action event from a JButton */ public class JButtonEventHandling extends JFrame { private final int WINDOW_WIDTH=300; //width of the frame private final int WINDOW_HEIGHT=150; //height of the frame private JPanel panel; //panel to hold components private JButton button; //a button public JButtonEventHandling(){ //set the title of the frame setTitle("JButton Event Handling"); //specify an action for the close operation setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the size of the frame setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //build panel buildPanel(); //add the panel to the frame add(panel); //show frame setVisible(true); } /* * This method creates a panel and add the button component */ public void buildPanel(){ //create a panel panel=new JPanel(); //create a button button=new JButton("change color"); //register the button with an action listener button.addActionListener(new ButtonListener()); //add button to the panel panel.add(button); } /* * Private inner class to respond action event from JButton */ private class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ panel.setBackground(Color.red); } } /* * The main method */ public static void main(String[] args) { //create an instance of the class new JButtonEventHandling(); } } |
Output
After clicking on the button, an action event is created and passed on to the actionPerformed method of the private inner class. This executes to change the color of the content pane of the JFrame to red.
Event Handlers must implement an interface
An event listener class must always implement an interface. An interface has abstract methods. The class that implements an interface must override all these abstract methods and provide implementation for the methods. When a class implements an interface it establishes a contract between the class and the interface. This contract forces the class to override all the abstract methods of the interface and provide implementation.
import statement for event handlers
Event listener interfaces are located in the java.awt.event package. Because of that, to use an event listener, you must precede your code with the import statement:
1 |
import java.awt.event.*; |
Note that the above import statement is a wildcard import statement. It makes all interfaces available for your program. You may however import specific interfaces only. Example: import java.awt.ActionListener imports the ActionListener interface and makes it available for your program.
The “implements” keyword
You use the implements keyword in a class header to indicate that the class implements an interface. This means, the class has established a contract between itself and the interface. That is, the class has agreed to override the methods of the interface.
In the code below, the ButtonListener class implements the ActionListener interface. This forces the ButtonListener class to have a method called actionPerformed. Note that this method was overwritten in the ActionListener interface. The class provides an implementation (body of code) for the actionPerformed method.
1 2 3 4 |
private class ButtonListener implements ActionListener{ //when button is clicked, code here executes if it is registered with the //JButton component. This sets the //background color of JPanel to read } |
Similarly, the class CheckBoxListener implements the interface ItemListener and thus, overwrites its method, ItemStateChanged, and provides implementation for it.There is always a contract between the class and the interface it implements and the class must honor this contract by providing implementation for all the methods of the interface.
1 2 3 4 5 |
private class CheckBoxListener implements ItemListener{ public void itemStateChanged(ItemEvent e){ //code here executes if the JCheckBox is registered with the listener } } |
Recent Comments