javax swing events and GUIs l GUI programming

  • Slides: 18
Download presentation
javax. swing, events, and GUIs l GUI programming requires processing events Ø There’s no

javax. swing, events, and GUIs l GUI programming requires processing events Ø There’s no visible loop in the program Ø Wire up/connect widgets, some generate events, some process events • Pressing this button causes the following to happen Ø We want to practice “safe wiring”, meaning? l We need to put widgets together, what makes a good userinterface? What makes a bad user-interface? Ø How do we lay widgets out, by hand? Using a GUI-builder? Using a layout manager? l How do we cope with widget complexity? Project JETT/Duke 1

Wiring Buttons and Events and GUIs l l l Button/Menu generates event Ø Action.

Wiring Buttons and Events and GUIs l l l Button/Menu generates event Ø Action. Event Click mouse generates event Ø Mouse. Event When creating widget/button Ø Attach listeners for events Ø Can have multiple listeners Activating widget/button Ø Causes listeners to “hear” Ø Listeners act accordingly Press E, handle E-event Ø What happens? Press A, handle A-event … Project JETT/Duke E A 2

Action and other events l l Widgets generate events, these events are processed by

Action and other events l l Widgets generate events, these events are processed by event listeners Ø Different types of events for different scenarios: press button, release button, drag mouse, press mouse button, release mouse button, edit text in field, check radio button, … Ø Some widgets “fire” events, some widgets “listen” for events To process events, add a listener to the widget, when the widget changes, or fires, its listeners are automatically notified. Ø Firing widget is the “model”, listener is the “view” aka observable and observer, respectively Project JETT/Duke 3

Code to wire widgets/events/listeners l Old-fashioned way, make the GUI the listener – has

Code to wire widgets/events/listeners l Old-fashioned way, make the GUI the listener – has do. Next() Ø Listener is an object, with the right method (kind of event) my. Next. Button = new JButton("next"); my. Next. Button. add. Action. Listener(this); public void action. Performed(Action. Event e) { if (e. get. Source() == my. Next. Button) do. Next(); } l New way, create anonymous event-listener object/on-the-fly Ø Inner class, has access to/can call GUI functions my. Next. Button. add. Action. Listener( new Action. Listener(){ public void action. Performed(Action. Event e) { do. Next(); } }); Project JETT/Duke 4

Adding Listeners l In lots of code you’ll see that the Container widget is

Adding Listeners l In lots of code you’ll see that the Container widget is the listener, so pressing a button or selecting a menu is processed by the Container/Frame’s action. Performed method Ø All Action. Listeners have an action. Performed method, is this interface/implements or inheritance/extends? Ø Here’s some “typical” code, why is this bad? void action. Performed(Action. Event e) { if (e. get. Source() == this. Button) … else if (e. get. Source() == that. Menu)… } Project JETT/Duke 5

A GUI object can be its own client l Occasionally a GUI will be

A GUI object can be its own client l Occasionally a GUI will be a listener of events it generates Ø Simple, but not extendable Ø Inner classes can be the listeners, arguably the GUI is still listening to itself, but … • Encapsulating the listeners in separate classes is better l Client (non. GUI) objects cannot access GUI components Ø Properly encapsulated JText. Field, for example, responds to a. Gui. display. Text(), Ø l Tension: simplicity vs. generality Don’t wire widgets together directly, use methods/classes Ø Eventual trouble when GUI changes Project JETT/Duke 6

Using inner/anonymous classes l l For each action/event that must be processed, create an

Using inner/anonymous classes l l For each action/event that must be processed, create an object to do the processing Ø Command pattern: parameterize object by an action to perform, queue up requests to be executed at different times, support undo Ø There is a javax. swing Event Queue for processing events, this is the hidden while loop in event processing GUI programs The inner class can be named, or it can be created “anonymously” Ø For reuse in other contexts, sometimes naming helpful Ø Anonymous classes created close to use, easy to read (arguable to some) Project JETT/Duke 7

Listeners l Events propagate in a Java GUI as part of the event thread

Listeners l Events propagate in a Java GUI as part of the event thread Ø Don’t manipulate GUI components directly, use the event thread Ø Listeners/widgets register themselves as interested in particular events • Events go only to registered listeners, can be forwarded/consumed l Action. Listener, Key. Listener, Item. Listener, Mouse. Motion. Listener, …, see java. awt. event. * Ø Isolate listeners as separate classes, mediators between GUI, Controller, Application Ø Anonymous classes can help here too Project JETT/Duke 8

JComponent/Container/Component l The java. awt package was the original widget package, it persists as

JComponent/Container/Component l The java. awt package was the original widget package, it persists as parent package/classes of javax. swing widgets Ø Most widgets are JComponents (subclasses), to be used they must be placed in a Container • The former is a swing widget, the latter awt: Jconvention. l A Container is also a Component Ø JFrame is often the “big container” that holds all the GUI widgets, we’ll use this and JApplet (awt counterparts are Frame and Applet) Ø A JPanel is a JComponent that is also a Container • Holds JComponents, for example and is holdable as well Project JETT/Duke 9

Creating a GUI l l l Create Frame (applet) Ø Application/Applet Create main panel

Creating a GUI l l l Create Frame (applet) Ø Application/Applet Create main panel Ø Border. Layout Create button panels Ø Top/Bottom, wired Create menus Ø Wired when created Create main view/panel Ø Shows what’s happening Create non-view logic and attach appropriately Ø Debug 5 days and serve Project JETT/Duke FILE B 1 HELP B 2 B 3 Main Panel 10

Creating a GUI (again) l l l Create Frame (applet) Ø Application/Applet Create main

Creating a GUI (again) l l l Create Frame (applet) Ø Application/Applet Create main panel Ø Border. Layout Create button panels Ø Top/Bottom, wired Create menus Ø Wired when created Create main view/panel Ø Shows what’s happening Create non-view logic and attach appropriately Ø Debug 5 days and serve Project JETT/Duke Main Panel 11

Widget layout l A Layout Manager “decides” how widgets are arranged in a Container

Widget layout l A Layout Manager “decides” how widgets are arranged in a Container Ø In a JFrame, we use the Content. Pane for holding widgets/components, not the JFrame itself Ø Strategy pattern: “related classes only differ in behavior, configure a class with different behaviors… you need variants of an algorithm reflecting different constraints…context forwards requests to strategy, clients create strategy for the context” • Context == JFrame/container, Strategy == Layout l Layouts: Border, Flow, Grid. Bag, Spring, … Ø I’ll use Border, Flow, Grid in my code Project JETT/Duke 12

Border. Layout (see Browser. java) l Default for the JFrame content pane l Provides

Border. Layout (see Browser. java) l Default for the JFrame content pane l Provides four areas, center is “main area” for resizing l Recursively nest for building complex (yet simple) GUIs l Border. Layout. CENTER for adding components Ø Some code uses “center”, bad idea (bugs? ) Project JETT/Duke NORTH W E S T CENTER E A S T SOUTH 13

JFrame/JApplet things to remember l set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); l Sometimes see Window.

JFrame/JApplet things to remember l set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); l Sometimes see Window. Listener, previous JDK Ø Not used in applets set. Content. Pane(panel. With. Everything); l Create panel, add to top-level content pane set. Visible(true); Ø Ø Ø Must have this in application l JFrame Ø set. Size(x, y); pack(); set. Title(. . ) l JApplet Ø init(); arguments via get. Parameter(. . ) Project JETT/Duke 14

Model, View, Controller overview l Model, View, Controller is MVC Ø Model stores and

Model, View, Controller overview l Model, View, Controller is MVC Ø Model stores and updates state of application • Example: calculator, what's the state of a GUI-calculator? Ø When model changes it notifies its views • Example: pressing a button on calculator, what happens? Ø The controller interprets commands, e. g. , button-click, forwards them appropriately to model (not to view) • Example: code for calculator that reacts to button presses Controller isn't always a separate class, often part of GUIbased view in M/VC MVC is a fundamental design pattern: solution to a problem at a general level, not specific code per se Ø l Project JETT/Duke 15

MVC in Same. Game/Clickomania l Clicking on a piece connected to more than one

MVC in Same. Game/Clickomania l Clicking on a piece connected to more than one causes pieces to be removed Ø See Game. Gui, Game. Core, Game. Model, Game. Applet, Game. Move l The model “knows” the location of the pieces Ø Determines if move is legal Ø Makes a move (records it) and updates views l View shows a board and information, e. g. , undo possible Ø See IGame. View interface, implemented by application and applet via Game. Core Project JETT/Duke 16

Same. Game MVC: Controller l Here the Game. Core communicates with model so acts

Same. Game MVC: Controller l Here the Game. Core communicates with model so acts as controller together with Button. Panel Ø Sometimes a middleman class isn’t a good idea, extra layer of code that might not be needed Ø Often in MVC model communicates with multiple views, but communication to model via controller l Game. Core implements IGame. View, easy to incorporate new controllers/views Ø Model doesn’t know about implementation Ø Model doesn’t know about button panels Project JETT/Duke 17

Controller and Commands l Use-case for making a move: Ø Move generated, e. g.

Controller and Commands l Use-case for making a move: Ø Move generated, e. g. , by button-click Ø Move forwarded to model (by controller) • (If move is made, then undo state set to true in views) • If move is made, then views update board display l Use-case for undoing a move Ø Undo generated, e. g. , by button or menu choice Ø Undo forwarded to model (by controller) • If undo changes board, views will be updated • If future undo not possible, undo state set to false Project JETT/Duke 18