Object-Oriented Programming (Java), Unit 18 Kirk Scott 1
Inner Classes, Integrated Text Fields, Listeners, and Event Handling 18.1 Inner Classes 18.2 Integrated Text Fields, Listeners, and Event Handling 18.3 A More Flexible Design 2
18.1 Inner Classes Inner classes have access to the instance variables of the class that contains them. This example does nothing except illustrate that fact. 3
4
public class Echo4 { public static void main(String[] args) { MyOuterClass myOuterObject = new MyOuterClass(5); myOuterObject.makeInnerObject(); } 5
class MyOuterClass { private int outerClassVariable; public MyOuterClass(int inval) { outerClassVariable = inval; } public void makeInnerObject() { MyInnerClass innerObject = new MyInnerClass(); } private class MyInnerClass { private int innerClassVariable; public MyInnerClass() { innerClassVariable = outerClassVariable; } 6
18.2 Integrated Text Fields, Listeners, and Event Handling 7
8
Key points of this example relating to the text field and listener: The use and positioning of an integrated text field in order to do input. The use of a listener, which is the code which acts on the text field input. The implementation of the listener as an inner class of the frame class, so that it can easily pass around the input string. 9
The characteristics of the listener are defined by the fact that it implements a given interface. The listener can be an inner class of the frame class. At the same time it can be a component that is within the containment hierarchy of the frame class in the application. 10
Key points of this example relating to the panel: The panel is also implemented as an inner class of the frame. This emphasizes the characteristics of inner classes, and the direct access to instance variables that they afford, Implementing the panel in this way means that fewer method calls are needed in order to assign values to variables than would otherwise be needed. 11
In the long run, this is not a flexible design choice. In future examples the panel will not be implemented as an inner class. This will make it necessary to supply methods which allow the values of variables to be passed from one component to another as parameters. 12
☺☻☼♀♂♠♣♥♦♪♫ ♯ The most important point of this example overall: ☺☻☼♀♂♠♣♥♦♪♫ ♯ If you search the application code, you will not find a loop. However, the application behaves as if it contained a loop. Once begun, the application continues to run, and will echo successive inputs entered by the user. 13
This behavior is the result of the Java event handling mechanism. Effectively, this results from the use of a listener in the code. The user code never calls the listener. The Java system loops internally, detecting events. When it detects the type of event which the listener is designed to handle, the listener is called. 14
When the listener is finished running, the application is idle, but alive. The Java system continues to check for events. The application comes to an end when its top level container, the frame, is closed. 15
16
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Echo5 { public static void main(String[] args) { Echo5Frame myframe = new Echo5Frame(); myframe.setVisible(true); } class Echo5Frame extends JFrame { private String stringInQuestion = ""; private JTextField myField; private Echo5Panel myPanel; private final int FRAMEW = 500; private final int FRAMEH = 500; private final int STRINGX = 140; private final int STRINGY = 240; 17
public Echo5Frame() { setTitle("Echo5 Frame"); setSize(FRAMEW, FRAMEH); myField = new JTextField(); TextFieldListener myListener = new TextFieldListener(); myField.addActionListener(myListener); myPanel = new Echo5Panel(); Container contentPane = getContentPane(); contentPane.add(myPanel, "Center"); contentPane.add(myField, "North"); } 18
private class TextFieldListener implements ActionListener { public void actionPerformed(ActionEvent event) { stringInQuestion = myField.getText(); myField.setText(""); myPanel.repaint(); } 19
private class Echo5Panel extends JPanel { public Echo5Panel() { } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.paintComponent(g2); g2.drawString(stringInQuestion, STRINGX, STRINGY); } 20
18.3 A More Flexible Design This doesn’t differ in appearance. The difference is that the panel is no longer implemented as an inner class of the frame. This requires the addition of a new method and a call to it. 21
22
23
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Echo6 { public static void main(String[] args) { Echo6Frame myframe = new Echo6Frame(); myframe.setVisible(true); } class Echo6Frame extends JFrame { private JTextField myField; private Echo6Panel myPanel; private final int FRAMEW = 500; private final int FRAMEH = 500; 24
public Echo6Frame() { setTitle("Echo6 Frame"); setSize(FRAMEW, FRAMEH); myField = new JTextField(); TextFieldListener myListener = new TextFieldListener(); myField.addActionListener(myListener); myPanel = new Echo6Panel(); Container contentPane = getContentPane(); contentPane.add(myPanel, "Center"); contentPane.add(myField, "North"); } 25
private class TextFieldListener implements ActionListener { public void actionPerformed(ActionEvent event) { String inputString = myField.getText(); myPanel.setString(inputString); myField.setText(""); myPanel.repaint(); } 26
class Echo6Panel extends JPanel { private String stringInQuestion = ""; private final int STRINGX = 140; private final int STRINGY = 240; public Echo6Panel() { } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.paintComponent(g2); g2.drawString(stringInQuestion, STRINGX, STRINGY); } public void setString(String stringIn) { stringInQuestion = stringIn; } 27
The End 28