GUI Programming in Java Tim Mc Kenna SenecaYork

  • Slides: 16
Download presentation
GUI Programming in Java Tim Mc. Kenna Seneca@York

GUI Programming in Java Tim Mc. Kenna Seneca@York

GUI Programming Concepts l conventional programming: • sequence of operations is determined by the

GUI Programming Concepts l conventional programming: • sequence of operations is determined by the program • what you want to happen, happens when you want it l event-driven programming: • sequence of operations is determined by the user’s interaction with the application’s interface • anything that can happen, happens at any time

GUI Design Concepts l l l a wealth of information creates a poverty of

GUI Design Concepts l l l a wealth of information creates a poverty of attention - Herbert Simon Principles of good GUI Design IBM's Design concepts Saul Greenberg's HCI pages Tim's HCI notes

GUI Programming Concepts in Java l l l Java GUI ("Swing") has components Windows

GUI Programming Concepts in Java l l l Java GUI ("Swing") has components Windows GUI has controls Unix GUI has widgets examples: labels, buttons, check boxes, radio buttons, text input boxes, pull down lists Java Swing components: JLabel, JButton, JCheck. Box, JRadio. Button, JText. Field, JText. Area, JCombo. Box

Java GUI history: the AWT l l AWT(JDK 1. 0, 1. 1): Abstract Window

Java GUI history: the AWT l l AWT(JDK 1. 0, 1. 1): Abstract Window Toolkit package: java. awt, java. awt. event heavyweight components using native GUI system elements used for applets until most browsers supported JRE 1. 2

Swing in Java l l l Swing(Java 2, JDK 1. 2+) lightweight components that

Swing in Java l l l Swing(Java 2, JDK 1. 2+) lightweight components that do not rely on the native GUI or OS “look and feel” of Swing components • are identical on different platforms • can be customized Swing inherits from AWT still used for events, layouts

Swing Components in Java l l advanced GUI support. e. g. drag-and-drop package names:

Swing Components in Java l l advanced GUI support. e. g. drag-and-drop package names: javax. swing, javax. swing. event components inherit from JComponent components are added to a top-level container: JFrame, JDialog, or JApplet.

running a Swing application l java -Dswing. aatext=true My. Swing. Class • the option

running a Swing application l java -Dswing. aatext=true My. Swing. Class • the option sets the system property "swing. aatext" to "true" to enable antialiasing for every JComponent l l javaw runs a GUI without the console window e. g. Hello. World. Swing. java

Basic GUI Programming Steps in Java l l declare a container and components add

Basic GUI Programming Steps in Java l l declare a container and components add components to one or more containers using a layout manager register event listener(s) with the components create event listener method(s)

Basic GUI Programming Concepts in Java l Example: JFrame. Demo. TM. java, JFrame. Demo.

Basic GUI Programming Concepts in Java l Example: JFrame. Demo. TM. java, JFrame. Demo. TM 2. java, JFrame. Demo. java l container : a screen window/applet window/panel that groups and arranges GUI components l GUI component: an object with visual representation l Swing containers: JFrame, JApplet, JPanel l AWT containers: Frame, Applet, Panel

GUI Programming: The Java Approach l event-driven programming • a piece of code (i.

GUI Programming: The Java Approach l event-driven programming • a piece of code (i. e. event handler) is attached to a GUI component • an event handler is called when an event (e. g. a mouse click) is activated / fired l The Delegation Event Model in Java • processing of an event is delegated to an object (the listener) in the program

Event-driven Programming in Java l event source: a GUI component that generates / fires

Event-driven Programming in Java l event source: a GUI component that generates / fires an event l event: a user interaction (e. g. a click on the button) l event listener: an object that has implemented event handlers to react to an event

Event Handling in Java l the delegation event model - event listeners must be

Event Handling in Java l the delegation event model - event listeners must be registered with an event source in order to receive notification

Event Handling in Java l registration of an event listener - write a class

Event Handling in Java l registration of an event listener - write a class that implements an [event type]Listener interface - create an instance of that class (i. e. an event listener) - register the listener with a GUI component: add[event type]Listener ( <an event listener> )

Event Handling in Java l l a listener interface has a list of standard

Event Handling in Java l l a listener interface has a list of standard event handlers (i. e. methods) API documentation - java. awt. event - event classes - listener interfaces - adapter classes

Event Handling in Java l l different ways of coding the event listeners JFrame.

Event Handling in Java l l different ways of coding the event listeners JFrame. Demo. TM. java uses named inner classes. JFrame. Demo. TM 2. java uses named inner classes and shows how to consolidate window closing from two different events. JFrame. Demo. java does not use inner classes.