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

Slides:



Advertisements
Similar presentations
1 Object-Oriented Programming (Java), Unit 20 Kirk Scott.
Advertisements

Event Handling Events and Listeners Timers and Animation.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Event-Driven Programming
ECP4136 Java Technology Tutorial 4. Class A class is a blueprint of an object The class represents the concept of an object, and any object created from.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
James Tam An introduction into HCI: Task-Centered System Design An Introduction To Graphical User Interfaces The event-driven model Building a simple.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
1 Object-Oriented Programming (Java), Unit 22 Kirk Scott.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Unit 20 Factory Method Summary prepared by Kirk Scott 1.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Object-Oriented Programming (Java), Unit 26 Kirk Scott 1.
GUI programming Graphical user interface-based programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Object-Oriented Programming (Java), Unit 26 Kirk Scott 1.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 Object-Oriented Programming (Java), Unit 23 Kirk Scott.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
1 Object-Oriented Programming (Java), Unit 17 Kirk Scott.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Object-Oriented Programming (Java), Unit 17 Kirk Scott.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
A simple swing example GETTING STARTED WITH WIND CHILL.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
Multiple buttons and action calls
Modular Event Handling
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces -- GUIs
Java Graphics.
Object-Oriented Programming (Java), Unit 22
A First Look at GUI Applications
Object-Oriented Programming (Java), Unit 23
A Quick Java Swing Tutorial
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

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