graphical user interfaces · graphical user interfaces 7 january 2019 osu cse 1. programs with guis...

47
Graphical User Interfaces 7 January 2019 OSU CSE 1

Upload: others

Post on 23-May-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Graphical User Interfaces

7 January 2019 OSU CSE 1

Page 2: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Programs With GUIs

• A Java program with a GUI, or graphical user interface, is pretty routine in most respects– It declares and manipulates the values of

some variables of various types, albeit new ones intended for use in developing GUIs (e.g., buttons, scrollbars, drawing panels, etc.)

• There is just one (big) issue...

7 January 2019 OSU CSE 2

gooey

Page 3: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

The User Interaction Problem• Not just your program, but an end-user, can

spontaneously change the “state” of any active user interface widget (e.g., click a button, check a box, move a slider, scroll a document, press a key, etc.)

• Problem: How does your program know whenthe user has attempted to provide input to the program via a widget, and determine whichwidget has been manipulated?

7 January 2019 OSU CSE 3

Page 4: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

The User Interaction Problem• Not just your program, but an end-user, can

spontaneously change the “state” of any active user interface widget (e.g., click a button, check a box, move a slider, scroll a document, press a key, etc.)

• Problem: How does your program know whenthe user has attempted to provide input to the program via a widget, and determine whichwidget has been manipulated?

User interaction includes the keyboard—and any other input

devices, e.g., a Kinect controller; so, it goes well beyond reading

characters using a SimpleReader.

7 January 2019 OSU CSE 4

Page 5: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Terminology

• The act of a user manipulating a widget is called an event for that widget

• The widget the user has manipulated is called the subject of the interaction

• The objects in your program that need to do something in response to the events for a particular subject are called observers(or listeners) for that subject

7 January 2019 OSU CSE 5

Page 6: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Solution #1: Use Polling

• The main program (the only observer) continually polls each possible subject to ask whether any events have occurred

• This is considered cumbersome...

subject observer

7 January 2019 OSU CSE 6

Page 7: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Polling Pseudo-codewhile (true) {

if (s0 has experienced an event) {if (event is e0) {

respond to it} else if (event is e1) {respond to it

} else ...} else if (s1 has experienced an event) {...

}}

77 January 2019 OSU CSE

repeatedly asking did you experience an event

Page 8: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Solution #2: Use Callbacks

• Each observer (there may be many) registers its interest in a subject’s events, and then waits until the subject calls it back to tell it that there has been an event

subject observer

event

7 January 2019 OSU CSE 8

alsocalled

interrupts

Page 9: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Solution #2: Use Callbacks

• Each observer (there may be many) registers its interest in a subject’s events, and then waits until the subject calls it back to tell it that there has been an event

subject observer

event

7 January 2019 OSU CSE 9

But how? What does this mean?

Page 10: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

The Observer Pattern• Each subject expects each observer

(listener) to register itself with that subject if it is interested in the subject’s events

• Each subject keeps track of its own set of interested observers

• Whenever an event occurs, the subject invokes a specific callback method for each registered observer, passing an event argument that describes the event

7 January 2019 OSU CSE 10

Page 11: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

The Observer Pattern• Each subject expects each observer

(listener) to register itself with that subject if it is interested in the subject’s events

• Each subject keeps track of its own set of interested observers

• Whenever an event occurs, the subject invokes a specific callback method for each registered observer, passing an event argument that describes the event

7 January 2019 OSU CSE 11

Registering interest is done by calling a method of the

subject; usually this is done once as part of set-up.

Page 12: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

The Observer Pattern• Each subject expects each observer

(listener) to register itself with that subject if it is interested in the subject’s events

• Each subject keeps track of its own set of interested observers

• Whenever an event occurs, the subject invokes a specific callback method for each registered observer, passing an event argument that describes the event

7 January 2019 OSU CSE 12

The set of observers for a given subject can be kept in a Set variable, for example.

Page 13: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

The Observer Pattern• Each subject expects each observer

(listener) to register itself with that subject if it is interested in the subject’s events

• Each subject keeps track of its own set of interested observers

• Whenever an event occurs, the subject invokes a specific callback method for each registered observer, passing an event argument that describes the event

7 January 2019 OSU CSE 13

This method is described in an interface that any

potential observer must implement.

Page 14: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

The Observer Pattern• Each subject expects each observer

(listener) to register itself with that subject if it is interested in the subject’s events

• Each subject keeps track of its own set of interested observers

• Whenever an event occurs, the subject invokes a specific callback method for each registered observer, passing an event argument that describes the event

7 January 2019 OSU CSE 14

This is one of many object-oriented design patterns that address common OOP issues (often

language deficiencies); most are considered best practices.

Page 15: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Example: Simple GUI Demo

ActionListener

implements

JFrame

extends

DemoGUI

7 January 2019 OSU CSE 15

Page 16: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Example: Simple GUI DemoComponents from Java’s Swing Framework

implements

JFrame

extends

DemoGUI

7 January 2019 OSU CSE 16

ActionListener

Page 17: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Components from Java’s Swing Framework

Example: Simple GUI Demo

implements

JFrame

extends

DemoGUI

7 January 2019 OSU CSE 17

This class is the type of the main window of a

GUI application.

ActionListener

Page 18: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Components from Java’s Swing Framework

Example: Simple GUI Demo

implements

JFrame

extends

DemoGUI

7 January 2019 OSU CSE 18

This interface declares the callback method: actionPerformed.

ActionListener

Page 19: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Components from Java’s Swing Framework

Example: Simple GUI Demo

implements

JFrame

extends

DemoGUI

7 January 2019 OSU CSE 19

ActionListener

This class (our code) contains the body of the actionPerformed

method, based on the program’s intent, and other

code to set up the GUI.

Page 20: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Important Interfaces/Methodsinterface ActionListener {

void actionPerformed(ActionEvent e);

}

interface ActionEvent {

Object getSource();

...

}

7 January 2019 OSU CSE 20

Page 21: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Important Interfaces/Methodsinterface ActionListener {

void actionPerformed(ActionEvent e);

}

interface ActionEvent {

Object getSource();

...

}

7 January 2019 OSU CSE 21

The class Object is special in Java: every class extends Object! We will

return to this later...

Page 22: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Fundamentals: DemoGUI

JButton

JTextArea in JScrollPane

JFrame

JPanel

7 January 2019 OSU CSE 22

Page 23: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Fundamentals: DemoGUI

JButton

JTextArea in JScrollPane

JFrame

JPanel

This is the underlying type of the main window of a GUI application using Swing.

7 January 2019 OSU CSE 23

Page 24: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Fundamentals: DemoGUI

JButton

JTextArea in JScrollPane

JFrame

JPanel

Nested inside a JFrame’s content pane, you can put any number of things …

7 January 2019 OSU CSE 24

Page 25: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Fundamentals: DemoGUI

JButton

JTextArea in JScrollPane

JFrame

JPanel

… such as a JPanel …

7 January 2019 OSU CSE 25

Page 26: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Fundamentals: DemoGUI

JButton

JTextArea in JScrollPane

JFrame

JPanel

… and nested inside a JPanel, you can put any number of, e.g., JButtons.

7 January 2019 OSU CSE 26

Items

Page 27: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Fundamentals: DemoGUI

JButton

JTextArea in JScrollPane

JFrame

JPanel

You can also put in a JFrame a JScrollPane with, e.g., a JTextArea.

7 January 2019 OSU CSE 27

Page 28: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

It’s Demo Time

• The DemoGUI1 project contains a very simple GUI application using Swing

• You can get it at:http://cse.osu.edu/software/common/DemoGUI1.zip

7 January 2019 OSU CSE 28

Showeclipse

Page 29: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Set Up by DemoGUI Constructor

7 January 2019 OSU CSE 29

Page 30: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Set Up by DemoGUI Constructor

7 January 2019 OSU CSE 30

Before registration of this as an observer of the

buttons.

Page 31: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Set Up by DemoGUI Constructor

7 January 2019 OSU CSE 31

After registration of this as an observer of the

buttons.

there is moreinsidethan justthis referenceString visibleTextetc

Page 32: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Instance Variables• Variables can be declared:

– in method bodies: local variables– in method headers: formal parameters– in classes: fields or instance variables

• Examples of instance variables:– resetButton, copyButton, inputText, outputText,

input, output• Instance variables are essentially global

variables that are shared by and can be accessed from all instance methods in the class

7 January 2019 OSU CSE 32

or globalvariables

or classvariables

depends on the visibility

Page 33: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Now, Who’s In Charge?

• Note: when DemoGUI is executed:– DemoGUI.main starts execution

• Constructor for DemoGUI is called by main• Constructor for DemoGUI returns to main

– DemoGUI.main finishes execution• After that, what code is executing?

7 January 2019 OSU CSE 33

Page 34: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Threads

• A standard Java program executes in a thread, i.e., a single path of sequential code executing one step at a time

• A GUI program with Swing uses at least two threads rather than one:– The initial thread executes main (until it

completes)– An event dispatch thread executes

everything else, including actionPerformed

7 January 2019 OSU CSE 34

all of ourprograms

have

be S thti threaded P Y POP P

Page 35: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 35

main

time

main thread starts

Page 36: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 36

main

This is the initial thread; main

executes...

time

Page 37: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 37

DemoGUI

... until it calls the DemoGUI

constructor, which executes...

time

Page 38: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 38

DemoGUI

Swing code

... until it calls the JFrameconstructor (super), which

starts Swing code executing in the event dispatch thread...

timeevent dispatchthread starts

Page 39: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 39

DemoGUI

... and the DemoGUIconstructor continues

until it returns to main...

time

Swing code

Page 40: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 40

main

... and maincontinues until it

completes; end of initial thread.

time

Swing code

Page 41: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 41

The event dispatch thread is

now the only executing thread.

time

Swing code

Page 42: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 42

An event results in a call to

actionPerformed...

actionPerf

time

Page 43: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Timeline of Thread Execution

7 January 2019 OSU CSE 43

... and when it returns, the Swing code

resumes; and so on.

time

Swing code

Page 44: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Layout Managers

• A layout manager allows you to arrange widgets without providing specific location coordinates– GridLayout (simplest?; used in DemoGUI)– FlowLayout (default for JPanel)– BorderLayout (default for JFrame)– ...

7 January 2019 OSU CSE 44

Page 45: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Java GUI Packages

• Some important packages in the Java libraries for GUI components:– java.awt– java.awt.event– javax.swing– ...

7 January 2019 OSU CSE 45

Page 46: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Java Swing Widgets• Some important classes in javax.swing:

– JFrame– JPanel– JButton– JScrollPane– JTextArea– JCheckBox– JComboBox– ...

7 January 2019 OSU CSE 46

Page 47: Graphical User Interfaces · Graphical User Interfaces 7 January 2019 OSU CSE 1. Programs With GUIs • A Java program with a GUI, or graphical user interface, is pretty routine in

Resources

• Java Tutorials (and beyond...)– http://docs.oracle.com/javase/tutorial/uiswing/index.html

• A Visual Guide to Layout Managers– http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

7 January 2019 OSU CSE 47