Unformatted text preview:

1Java Swing, EventsReadings:Just Java 2: Chap 19 & 21, orEckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing ScienceUniversity of Alberta2Java Foundation Classes• JFC:– Abstract Window Toolkit (AWT)– original user interface toolkit– don’t go there!– Swing– package javax.swing.*, introduced in Java 1.23Swing• Portable API:– The appearance and behavior (look-and-feel) of the user interface components areimplemented in Java …– might work slightly differently from any host platform– pluggable look-and-feelse.g., Motif, windows,…4Containment Hierarchy• Top-level container:– place for other Swing components to paint themselves– e.g., JFrame, JDialog, Japplet• Intermediate container:– simplify positioning of atomic components– e.g., JPanel, JSplitPane, JTabbedPane5Containment Hierarchy• Atomic components:– self-sufficient components that present information to and get input from the user– e.g., JButton, JLabel, JComboBox, JTextField, JTable6Swing• Componentsand containers:– superclassesand interfaces– extendsand implements© O’Reilly 199927Swing• Java Documentation:– http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/package-summary.html• SwingSet:– http://java.sun.com/products/javawebstart/demos.html• Quick tutorial:– http://java.sun.com/docs/books/tutorial/uiswing/start/swingTour.html8Containers• Notes:– Container objects group components, arranging them for display with a layout manager.9Top-Level Containers• JFrame example:– contains a single component JRootPane, which has a JMenuBar (optional) and a content pane– theFrame.setJMenuBar( theMenuBar )– theFrame.setContentPane( thePanel )– add non-menu components to this content pane– theFrame.getContentPane().add( aButton )10Events• Two approaches to event handling– read-evaluation loop (client-written loop)– notification-based (callbacks)• Swing uses the 2nd approach11Events• Swing:– objects communicate by “firing” and “handling” events (event objects)– (conventional method call)– events are sent from a single source object to one or more registered listener objects12Events• Swing:– different event sources produce different kinds of eventse.g., a JButton object, when clicked, generates an ActionEvent object, which is handled by an ActionListener (an object whose class implements this interface)313Eventsevent source event listenerlistener interface1..*1..*14Events• Handling:– create a component– e.g., a JButton– add it to the GUI– e.g., to a JPanel– register a listener to be notified when the component generates an event– e.g., interface ActionListener– define the callback method– e.g., actionPerformed()15Event Handling• class MyListener implements ActionListener {…public void actionPerformed( ActionEvent event ) {// react to event…}} • …// instantiate event listenerActionListener listener = new MyListener();…// instantiate event sourceJButton button = new JButton( “Hello” );…// register event listener with event sourcebutton.addActionListener( listener );16UML Sequence Diagram:MyPanel:JButton:MyListenernewaddActionListener ()…newactionPerformed ()17Event Handling• Options for implementing listeners:– listener class– anonymous inner classes– named inner classes18Event Handling• Listener class:419Event Handling• Anonymous inner listener class:20Event Handling• Named inner listener class:21Event Handling• Note:– A class could potentially be both an event source and event listener.– Good or bad idea? …22Event Handling• public class MyButton extends JButton implementsActionListener {…public MyButton() {…addActionListener( this );}…public void actionPerformed( ActionEvent event ) {…}}• JButton button = new MyButton() …23Dependencies24Dependencies• Problems:– need to maintain consistency in the views (or observers)– need to update multiple views of the common data model (or subject)– need clear, separate responsibilities for presentation (look), interaction (feel), computation, persistence525Model/View/Controller• MVC roles:– model– complete, self-contained representation of object managed by the applicatione.g., spreadsheet document– provides a number of services to manipulate the datae.g., recalculate, save– computation and persistence issues– …26Model/View/Controller• MVC roles:– view– tracks what is needed for a particular perspective of the datae.g., bar chart view– presentation issues– controller– gets input from the user, and uses appropriate information from the view to modify the modele.g., get slider value, trigger chart modify– interaction issues27Model/View/Controller28Model/View/Controller• Separation:– you can modify or create views without affecting the underlying model– the model should not need to know about all the kinds of views and interaction styles available for it– separate threads?29Model/View/Controller• In Swing:– in practice, views and controllers are implemented with Swing components and listeners– both views and controllers will be dependent on Swing APIs30Model/View/Controller• In Swing:– still, try to separate the model and its services so that it is Swing-free– model is like a “virtual machine” or “kernel” specific to the application631Model/View/Controller• Smalltalk:– originated the MVC concept– integral support in interactive applications with MVC classes32Model/View/Controller• Java and Swing:– concept is still valid to help structure interactive applicationse.g., use a framework that supports MVC– Swing internally uses a variant of MVC for its pluggable look-and-feel capability …33Pluggable Look-and-Feel• Swing:– the look-and-feel is implemented in Java, but could mimic Windows, Motif, Classic, Aqua, etc.– UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”);– UIManager.setLookAndFeel(“javax.swing.plaf.metal.MetalLookAndFeel”);34Pluggable Look-and-Feel• SwingSet:– http://java.sun.com/products/javawebstart/demos.html35Pluggable Look-and-Feel• Idea:– similar to skins, themes, schemes, etc., but must include “feel” as well as “look”36Pluggable Look-and-Feel• Swing internals:– each component uses a user interfacedelegate object (responsible for view and controller roles)modelobjectuser interfaceobjectcomponentuser interfacemanager737Pluggable


View Full Document

UVA CS 4501 - Java Swing, Events

Download Java Swing, Events
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Swing, Events and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Swing, Events 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?