Lecture 18 Swing Introduction Lecturer Prof Jim Warren

  • Slides: 25
Download presentation
Lecture 18 – Swing Introduction Lecturer: Prof Jim Warren

Lecture 18 – Swing Introduction Lecturer: Prof Jim Warren

Outline • Modern Windowing/GUI environment • Welcome to Java Swing • Handling events

Outline • Modern Windowing/GUI environment • Welcome to Java Swing • Handling events

Desktop Environment • Uses Windows, Icons, Menus, Pointer (WIMP) to render a GUI •

Desktop Environment • Uses Windows, Icons, Menus, Pointer (WIMP) to render a GUI • Everything arranged on a desktop (desktop metaphor) • Different parts of a desktop environment (may be merged): – Windowing System: handles input/output – Widget Toolkit: draws widgets and dispatches their events – Window Manager: takes care of windows 3

Windowing System • Manages input and output devices: graphics cards, screens, mice, keyboards •

Windowing System • Manages input and output devices: graphics cards, screens, mice, keyboards • Redirects data from input devices to applications • Receives and processes drawing commands from applications • May be able to talk to remote applications: send input events and receive drawing commands over the network Mouse Keyboard Mouse up Windowing System Application 1 Draw circle Application 2 4

GUI Interaction Events Primitive Pointer Events • Mouse Moved • Mouse Down • Mouse

GUI Interaction Events Primitive Pointer Events • Mouse Moved • Mouse Down • Mouse Up Primitive Keyboard Events • Key Down • Key Up Complex Pointer Events • Click = mouse down, mouse up • Double Click = two clicks within a certain time • Enter = mouse moves into a region • Leave = mouse moves out of a region • Hover = mouse stays in region for a period of time • Drag and Drop = mouse down, mouse moved, mouse up 5 5

Input Handling in Widgets • Input events are dispatched to the right widgets by

Input Handling in Widgets • Input events are dispatched to the right widgets by windowing system and/or toolkit • Keyboard events are sent to widget with input focus in active window • Widgets have handlers for input events; they can translate simple input events into more complex, specific ones (e. g. “activated”) • Developers can set event handlers for widgets, which invoke application logic Windowing System Mouse up Mouse down Application 1 Button 1 Activated Paint 6

Rendering of Widgets have a visual representation • Widgets define “paint” event handler: draws

Rendering of Widgets have a visual representation • Widgets define “paint” event handler: draws the widget by sending commands to the windowing system • Widget gets “paint” (or “update”) events from the windowing system (possibly through toolkit) – Often not complete redrawing, but “update region” – Redrawing the update region is achieved with clipping • Application can send “invalidate” events to the windowing system if redrawing necessary (potentially triggers paint events) Update Region Button Activate green window Button 7

The GUI Event Loop 1. 2. 3. 4. GUI application is started Widgets are

The GUI Event Loop 1. 2. 3. 4. GUI application is started Widgets are set up Event loop is started Wait for events from the windowing system (event queue) 5. Dispatch each event to the right widget – Input event: call appropriate event handler ( call to application logic) – Paint event: call paint method 6. Go back to 4. Event-Driven Programming 8

Window Manager • Controls placement and appearance of windows (but not the window contents)

Window Manager • Controls placement and appearance of windows (but not the window contents) – Open, close, minimize, maximize, move, resize – Start apps, list and switch between running apps – Window decorators, desktop background with icons • Often built into windowing system • Implemented using a widget toolkit 9

Introduction to Java Swing 10

Introduction to Java Swing 10

AWT vs. Swing Abstract Windowing Toolkit (AWT) • Original Java GUI toolkit • Wrapper

AWT vs. Swing Abstract Windowing Toolkit (AWT) • Original Java GUI toolkit • Wrapper API for native GUI components • Lowest-common denominator for all Java host environments Swing • Implemented entirely in Java on top of AWT • Richer set of GUI components • Pluggable look-and-feel support 11

Swing Design Principles • GUI is built as containment hierarchy of widgets (i. e.

Swing Design Principles • GUI is built as containment hierarchy of widgets (i. e. the parent-child nesting relation between them) • Event objects and event listeners – Event object: is created when event occurs (e. g. click), contains additional info (e. g. mouse coordinates) – Event listener: object implementing an interface with an event handler method that gets an event object as argument • Separation of Model and View: – Model: the data that is presented by a widget – View: the actual presentation on the screen 12

Partial AWT and Swing Class Hierarchy java. lang. Object Checkbox. Group Button Canvas Checkbox

Partial AWT and Swing Class Hierarchy java. lang. Object Checkbox. Group Button Canvas Checkbox Component Choice Container JComponent Abstract. Button JLabel Menu. Component Label List Scrollpane JList JPanel JButton java. awt. * JScrollpane Scrollbar Text. Component Panel Window Applet Dialog Frame JApplet JDialog JFrame javax. swing. * 13

Swing Widgets Top-Level Containers (more on this next lecture) General-Purpose Containers JPanel JFrame JScroll.

Swing Widgets Top-Level Containers (more on this next lecture) General-Purpose Containers JPanel JFrame JScroll. Pane JDialog JSplit. Pane JTabbed. Pane JButton JCombobox JCheckbox JList JRadio. Button and Button. Group Menu JLabel 14

More Swing Widgets JFile. Chooser JTree JTable JColor. Chooser 15

More Swing Widgets JFile. Chooser JTree JTable JColor. Chooser 15

The Initial Swing GUI Containment Hierarchy a 3 D model enables menus to pop

The Initial Swing GUI Containment Hierarchy a 3 D model enables menus to pop up above the content pane Frame / Dialog / Applet Root Pane Layered Pane File Edit Undo Redo Cut allows for interception of mouse events and painting across GUI components Content Pane Glass Pane 16

The Initial Swing GUI Containment Hierarchy a. Top. Level. Container: JFrame or JDialog or

The Initial Swing GUI Containment Hierarchy a. Top. Level. Container: JFrame or JDialog or JApplet root. Pane: JRoot. Pane (JPanel) glass. Pane: java. awt. Component layered. Pane: JLayered. Pane (JPanel) content. Pane: java. awt. Container optional menu. Bar: JMenu. Bar 17

Swing Hello World import java. awt. *; import java. awt. event. *; import javax.

Swing Hello World import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Hello. World { public static void main(String[] args) { JFrame frame = new JFrame("Hello World!"); frame. set. Size(220, 200); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); Container content. Pane = frame. get. Content. Pane(); content. Pane. set. Layout(null); JButton button = new JButton("Hello World!"); button. set. Location(30, 30); button. set. Size(150, 100); content. Pane. add(button); frame. set. Visible(true); } } 18

Swing Hello World with Events. . . public class Hello. World { public static

Swing Hello World with Events. . . public class Hello. World { public static void main(String[] args) {. . . JButton button = new JButton("Hello World!"); button. add. Action. Listener(new My. Action. Listener()); . . . } } import java. awt. *; import java. awt. event. *; import javax. swing. *; public class My. Action. Listener implements Action. Listener { public void action. Performed(Action. Event e) { Toolkit. get. Default. Toolkit(). beep(); } } 19

… Containment Hierarchy of a Menu public class Menu. Example { public static void

… Containment Hierarchy of a Menu public class Menu. Example { public static void main(String[] args) { JFrame frame = new JFrame("My Frame"); frame. set. Default. Close. Operation( JFrame. EXIT_ON_CLOSE); JMenu file. Menu = file. Menu. add(new JMenu("File"); JMenu. Item("New")); JMenu. Item("Open")); JMenu. Item("Close")); JMenu edit. Menu = edit. Menu. add(new JMenu("Edit"); JMenu. Item("Undo")); JMenu. Item("Redo")); JMenu. Item("Cut")); JMenu. Bar menubar = new JMenu. Bar(); menubar. add(file. Menu); menubar. add(edit. Menu); frame. set. JMenu. Bar(menubar); frame. set. Visible(true); } } File Edit New Open Close File Edit Undo Redo Cut 20

Handling Menu Events. . . public class Menu. Example { static JFrame frame; public

Handling Menu Events. . . public class Menu. Example { static JFrame frame; public static void main(String[] args) {. . . JMenu. Item item = new JMenu. Item("Close"); item. add. Action. Listener(new Menu. Action. Listener()); file. Menu. add(item); . . . } } . . . public class Menu. Action. Listener implements Action. Listener { public void action. Performed(Action. Event e) { JOption. Pane. show. Message. Dialog(Menu. Example. frame, "Got an Action. Event at " + new Date(e. get. When()) + " from " + e. get. Source(). get. Class()); } } 21

Defining Event Listeners with Anonynous Classes . . . public class Menu. Example {

Defining Event Listeners with Anonynous Classes . . . public class Menu. Example { public static void main(String[] args) { final JFrame frame = new JFrame("My Frame"); . . . JMenu. Item item = new JMenu. Item("Close"); item. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { int n = JOption. Pane. show. Option. Dialog(frame, . . . } }); . . . } } • Use new Classname() {…} or new Interfacename(){…} to create a single object of an anonymous subclass of the given class/interface • Anonymous classes can access final variables of their context (i. e. final variables of the method or class they are created in) 22

Different Kinds of Swing Events Low-level events • Mouse. Event: Component got mouse-down, mouse-move,

Different Kinds of Swing Events Low-level events • Mouse. Event: Component got mouse-down, mouse-move, etc. • Key. Event: Component got key-press, key-release, etc. • Component. Event: Component resized, moved, etc. • Container. Event: Container's contents changed because a component was added or removed • Focus. Event: Component got focus or lost focus • Window. Event: Window opened, closed, etc. High-level semantic events • Action. Event: Main action of control invoked (e. g. JButton click) • Adjustment. Event: Value was adjusted (e. g. JScroll. Bar moved) • Item. Event: Item was selected or deselected (e. g. in JList) • Text. Event: Text in component has changed (e. g in JText. Field) 23

Events, Listeners, Adapters and Handler Methods Event Listener / Adapter Handler Methods Action. Event

Events, Listeners, Adapters and Handler Methods Event Listener / Adapter Handler Methods Action. Event Action. Listener action. Performed Adjustment. Event Adjustment. Listener adjustment. Value. Changed Mouse. Event Mouse. Listener Mouse. Adapter mouse. Clicked mouse. Entered mouse. Exited mouse. Pressed mouse. Released Key. Event Key. Listener Key. Adapter key. Pressed key. Released key. Typed Component. Event Component. Listener Component. Adapter component. Shown component. Hidden component. Moved component. Resized 24 Adapter classes with empty methods for Listener interfaces with >1 methods

Summary • Desktop environments consist of: – Windowing System: handles input/output – Widget Toolkit:

Summary • Desktop environments consist of: – Windowing System: handles input/output – Widget Toolkit: draws widgets and dispatches their events – Window Manager: takes care of windows • Swing is a widget toolkit for Java – GUI as containment hierarchy of widgets – Event objects and event listeners References: http: //java. sun. com/docs/books/tutorial/uiswing/ http: //www. javabeginner. com/java-swing-tutorial. htm 25