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 a wealth of information creates a poverty of attention - Herbert Simon l Principles of good GUI Design Principles of good GUI Design l IBM's Design conceptsDesign concepts l Saul Greenberg's HCI pagesHCI pages l Tim's HCI notesHCI notes
GUI Programming Concepts in Java l Java GUI ("Swing") has components l Windows GUI has controls l Unix GUI has widgets examples: labels, buttons, check boxes, radio buttons, text input boxes, pull down lists Java Swing components: JLabel, JButton, JCheckBox, JRadioButton, JTextField, JTextArea, JComboBox
Java GUI history: the AWT l AWT(JDK 1.0, 1.1): Abstract Window Toolkit l package: java.awt, java.awt.event l heavyweight components using native GUI system elements l used for applets until most browsers supported JRE 1.2
Swing in Java l Swing(Java 2, JDK 1.2+) l lightweight components that do not rely on the native GUI or OS l “look and feel” of Swing components are identical on different platforms can be customized l Swing inherits from AWT l AWT still used for events, layouts
Swing Components in Java l advanced GUI support. e.g. drag-and-drop l package names: javax.swing, javax.swing.event l components inherit from JComponent l components are added to a top-level container: JFrame, JDialog, or JApplet.
running a Swing application l java -Dswing.aatext=true MySwingClass the option sets the system property "swing.aatext" to "true" to enable anti- aliasing for every JComponent javaw runs a GUI without the console window l e.g. HelloWorldSwing.java
Basic GUI Programming Steps in Java l declare a container and components l add components to one or more containers using a layout manager l register event listener(s) with the components l create event listener method(s)
Basic GUI Programming Concepts in Java Example: JFrameDemoTM.java, JFrameDemoTM2.java, JFrameDemo.java container : a screen window/applet window/panel that groups and arranges GUI components GUI component: an object with visual representation Swing containers: JFrame, JApplet, JPanel AWT containers: Frame, Applet, Panel
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 event source: a GUI component that generates / fires an event event: a user interaction (e.g. a click on the button) 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 registered with an event source in order to receive notification
Event Handling in Java 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 ( )
Event Handling in Java 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 different ways of coding the event listeners l JFrameDemoTM.java uses named inner classes. l JFrameDemoTM2.java uses named inner classes and shows how to consolidate window closing from two different events. l JFrameDemo.java does not use inner classes.