2 GUI Programming Concepts conventional programming:sequence of operations is determined by the programwhat you want to happen, happens when you want itevent-driven programming:sequence of operations is determined by the user’s interaction with the application’s interfaceanything that can happen, happens at any time
3 GUI Design Conceptsa wealth of information creates a poverty of attention - Herbert SimonPrinciples of good GUI DesignIBM's Design conceptsSaul Greenberg's HCI pagesTim's HCI notes
4 GUI Programming Concepts in Java Java GUI has componentsWindows GUI has controlsUnix GUI has widgetsexamples: labels, buttons, check boxes, radio buttons, text input boxes, pull down listsSwing components: JLabel, JButton, JCheckBox, JRadioButton, JTextField, JTextArea, JComboBox
5 Java GUI history: the AWT AWT(JDK 1.0, 1.1): Abstract Window Toolkitpackage: java.awt, java.awt.eventheavyweight components using native GUI system elementsused for applets until most browsers supported JRE 1.2
6 Swing in JavaSwing(Java 2, JDK 1.2+)lightweight components that do not rely on the native GUI or OS“look and feel” of Swing componentsare identical on different platformscan be customizedSwing inherits from AWTAWT still used for events, layouts
7 Swing Components in Java advanced GUI support. e.g. drag-and-droppackage names: javax.swing, javax.swing.eventcomponents inherit from JComponentcomponents are added to a top-level container: JFrame, JDialog, or JApplet.
8 running a Swing application java -Dswing.aatext=true MySwingClassthe option sets the system property "swing.aatext" to "true" to enable anti-aliasing for every JComponentjavaw runs a GUI without the console window
9 Basic GUI Programming Steps in Java declare a container and componentsadd components to one or more containers using a layout managerregister event listener(s) with the componentscreate event listener method(s)
10 Basic GUI Programming Concepts in Java Example: JFrameDemo.java, JFrameDemoTM.javacontainer : a screen window/applet window/panel that groups and arranges GUI componentsGUI component: an object with visual representationSwing containers: JFrame, JApplet, JPanelAWT containers: Frame, Applet, Panel
11 GUI Programming: The Java Approach event-driven programminga piece of code (i.e. event handler) is attached to a GUI componentan event handler is called when an event (e.g. a mouse click) is activated / firedThe Delegation Event Model in Javaprocessing of an event is delegated to an object (the listener) in the program
12 Event-driven Programming in Java event source: a GUI component that generates / fires an eventevent: a user interaction (e.g. a click on the button)event listener: an object that has encapsulated event handlers to react to an event
13 Event Handling in Javathe delegation event model - a GUI element “delegates” the processing of an event to another piece of code (i.e. an event handler) the event source generates/fires an event and “sends” it to event listeners
14 Event Handling in Javathe delegation event model - event listeners must be registered with an event source in order to receive notificationExample: JButtonDemo.java, JButtonDemo2.java
15 Event Handling in Javaregistration of an event listener - write a class that implements an <event name>Listener interface create an instance of that class (i.e. an event listener) register the listener with a GUI component: add<event name>Listener ( <an event listener> )
16 Event Handling in Javaa listener interface has a list of standard event handlers (i.e. methods)API documentation java.awt.event event classes listener interfaces adapter classes
17 Event Handling in Javadifferent ways of coding the event listeners use of another top-level class: JButtonDemo - anonymous inner classes: JButtonDemo2, JFrameDemo, JFrameDemo2 - use of an adapter class: JFrameDemo3JFrameDemoTM2.java uses named inner classes and shows how to consolidate window closing from two different events.