Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

48
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1

Transcript of Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Page 1: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Object-Oriented Programming (Java), Unit 18

Kirk Scott

1

Page 2: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Wisdom the Albatross

• From news.discovery.com• Not many moms in their 60s can boast of

raising 30 offspring while having a new one in the nest. But such is the claim of the aptly named Wisdom, a Laysan albatross nesting in the South Pacific that is also the oldest known wild bird in the 90-year history of North American bird banding.

2

Page 3: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

3

Page 4: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

4

Page 5: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• “She looks great,” said Bruce Peterjohn, chief of the North American Bird Banding Program in a USGS news release.

5

Page 6: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• From seattletimes.com• Albatrosses mate for life, suggesting that

Wisdom probably had to find a new, younger mate maybe twice down the line. They work at a relationship, first by getting their groove on. “They dance together,” said Chandler Robbins, a retired senior scientist at USGS.

6

Page 7: 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

7

Page 8: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• This unit consists of 3 example programs, Echo4 through Echo6.

• These programs continue the process of integrating graphical user interface features into application programs.

8

Page 9: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

18.1 Inner Classes

• Inner classes were mentioned in a previous unit.

• Formally, a class is an inner class if the code defining it is completely contained in the code of another class.

• Inner classes have access to the instance variables of the class that contains them.

9

Page 10: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Echo4

• This example does nothing except illustrate the fact that inner classes have access to the instance variables of the classes that contain them.

• There is no screen shot for it, because there’s nothing to see.

• In future examples it will be seen how inner classes can be used when writing code for graphical user interfaces.

10

Page 11: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Echo4

-outerClassVariable : int

MyOuterClass

-innerClassVariable : int

MyInnerClass

-has1

1

-has1

1

11

Page 12: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• In the following code, note the default constructor for the inner class.

• Inside that constructor, the instance variable of the outer class is used when initializing the instance variable of the inner class.

• No get method is called, illustrating that the inner class has direct access to the instance variable of the outer class.

12

Page 13: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• public class Echo4• {• public static void main(String[] args)

• {• MyOuterClass myOuterObject = new MyOuterClass(5);

•myOuterObject.makeInnerObject();

• }• }

13

Page 14: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• 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;• }• }• }

14

Page 15: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

18.2 Integrated Text Fields, Listeners, and Event Handling

15

• A listener is a piece of code designed to detect an event that has occurred in a graphical user interface.

• If the listener is implemented as an inner class, its code can be simpler, because it has access to the instance variables of the class that contains it.

• Echo5 illustrates the use of a listener in an application with an integrated text field.

Page 16: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Echo5

16

Page 17: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• The screenshot illustrated that Echo5 is a genuine echoing program.

• The difference with Echo4 is that it has an integrated text field.

• Entering data into the text field triggers an event.• The following UML diagram shows that the text

field has a listener, but the listener is implemented as an inner class of the JFrame of the application overall.

17

Page 18: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Echo5

Echo5Frame

ContentPane

JFrame

+paintComponent()

Echo5PanelJPanel

-has1

1

-has1

1

-has

1

1

JTextField

Graphics

-has

1

1

String-has

1 1

-has11

+actionPerformed()

TextFieldListener

-has1 1

«interface»ActionListener

18

Page 19: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

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.

19

Page 20: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• The characteristics of the listener are defined by the fact that it implements a given interface.

• The code for the listener can be placed so that it’s an inner class of the frame class.

• At the same time, an instance of the listener can be created and associated with a text field, so the listener becomes a component that is within the containment hierarchy of the frame class in the application.

20

Page 21: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

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.

21

Page 22: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• 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.

22

Page 23: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

☺☻☼♀♂♠♣♥♦♪♫♯Pay Attention: This is the most important

point of this example overall:☺☻☼♀♂♠♣♥♦♪♫♯

• The previous echoing program examples all contained loops.

• If you search the code for Echo5, 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.

23

Page 24: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• 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.

24

Page 25: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• Calling the listener consists of calling the actionPerformed() method which the listener implements

• The actionPerformed() method contains the application specific code which is to be executed when the listener is called.

25

Page 26: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• It is important to understand that the call to the listener code in the application comes from outside of the program.

• It comes from the system.• When the listener is finished running, the

application is idle, but alive.

26

Page 27: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• The Java system continues to check for events and will trigger the listener if one occurs.

• The application comes to an end when its top level container, the frame, is closed.

• A sequence diagram of how this happens is shown on the following overhead.

27

Page 28: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

myListener myField myPanel g2

getText()

setText("")

stringInQuestion

actionPermformed()

repaint()update()

paintComponent()

super.paintComponent()

drawString(stringInQuestion)

28

Page 29: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• The code for Echo5 is shown on the following overhead.

• Note the implementation of the JPanel and the listener as inner classes.

• Especially note the definition of the listener and the actionPerformed() method that it contains.

29

Page 30: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• The point is this: The listener implements the ActionListener interface.

• This is what makes it a listener.• The ActionListener interface requires that

the implementing class have an actionPerformed() method.

30

Page 31: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• 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;

31

Page 32: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• 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");• }

32

Page 33: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• private class TextFieldListener implements ActionListener

• {• public void actionPerformed(ActionEvent event)

• {• stringInQuestion = myField.getText();

• myField.setText("");• myPanel.repaint();• }• }

33

Page 34: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• private class Echo5Panel extends JPanel• {• public Echo5Panel()• {• }• public void paintComponent(Graphics g)• {• Graphics2D g2 = (Graphics2D) g;• super.paintComponent(g2);• g2.drawString(stringInQuestion,

STRINGX, STRINGY);• }• }• }

34

Page 35: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

18.3 A More Flexible Design

• The graphical appearance of Echo6 doesn’t differ from that of Echo5.

• 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 in order to pass around the String to be echoed.

• In spite of this extra work, not having the JPanel be an inner class is a more flexible design in the long run.

35

Page 36: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Echo6

36

Page 37: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• The UML diagram shown on the next overhead is the similar to the UML diagram for Echo5

• The JPanel class is not an inner class.• Also, the setString() method that is needed

is shown as an operation in the JPanel class.

37

Page 38: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Echo6

Echo6Frame

ContentPane

JFrame

+paintComponent()+setString()

Echo6Panel

+paintComponent()+repaint()

JPanel

-has1

1

-has1

1

-has added

1

1

JTextField

Graphics

-has

1

1

String

-has

1

1

-has

1

1

+actionPerformed()

TextFieldListener

-has1

1

-has instance variable

1

1

«interface»ActionListener

38

Page 39: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• The sequence diagram on the following overhead shows how a call to setString() is needed in the process of echoing input from the text field as output in the panel.

• Except for this call, the rest of the calls remain the same as before.

39

Page 40: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

myListener myField myPanel g2

getText()

setText("")

inputString

actionPermformed()

setString(inputString)

update()

paintComponent()

super.paintComponent()

drawString(stringInQuestion)

repaint()

40

Page 41: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• The code for Echo6 follows.• It is quite similar to the code for Echo5.• Assuming Echo5 was reasonably clear,

the main point is identifying where the code for Echo6 differs from it.

41

Page 42: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• 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;

42

Page 43: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• 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");• }

43

Page 44: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• private class TextFieldListener implements ActionListener

• {• public void

actionPerformed(ActionEvent event)• {• String inputString =

myField.getText();• myPanel.setString(inputString);• myField.setText("");• myPanel.repaint();• }• }• }

44

Page 45: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• 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;• }• }

45

Page 46: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

Unit 18 Assignment

• The previous unit ended with a description of the assignment.

• That unit and that assignment began the middle part of the course, where versions of Wari may or may not be given, and the assignment for every unit is to implement Togiz Kumalak with the features discussed in the unit.

46

Page 47: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

• Having been shown the plan in Unit 17, for this unit and the following ones, typically, there is no need to preview the assignment in the overheads.

• You know, in principle, what it will always be…

47

Page 48: Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.

The End

48