Download presentation
Presentation is loading. Please wait.
Published byMerry Mason Modified over 9 years ago
1
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Programming Object-Oriented GUIs II Swing Components and Events SOFTENG 350 – Lecture 17
2
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Organizational Stuff Lab on Thursday 10-12. Now: GTL and FTL (CS Building) Next lab on 11 th May: Swing exercises. –Layout manager –GUI components: lists, text, buttons, dialogs Lab next week, on 19 th May: guided tour to JSP technology –Downloading, installing and running a Tomcat web server –Implementing and deploying a first Java Servlet –Implementing and deploying a first Java Server Page
3
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Don’t call us, we’ll call you ! Summary The Hollywood-Principle (don’t call us we call you) means the OO call back mechanism: the way object-oriented APIs are customized by developers. The developer (i) extends API classes and overrides template methods or (ii) implements interfaces. The developer then instantiates objects and passes them in method calls to the API. Don’t call us and we won’t call you. With Objects, subtype polymorphism and dynamic binding OOP languages provide a typed, higher-order parameter passing mechanism. Code can be encapsulated in an object and therefore passed as a parameter in a type-safe manner.
4
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Don’t ask what kind ! Summary If you have some objects that are used identical but behave differently, make their classes implement a common interface. The interface can be seen as a contract or protocol the single objects adhere to. In client code that uses the objects, use the objects as objects of their common supertype. The OOP dynamic binding mechanism knows the correct type and executes the correct service. You do not have to clutter up your service code with asking for types. The defined common interface serves as an inbuilt documentation that is kept under surveillance of the OOP language type checker. You cannot introduce a new subclass without implementing all the methods of the interface.
5
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Partial AWT and Swing Class Hierarchy java.lang.Object ComponentMenuComponentCheckboxGroup ButtonCheckboxCanvasChoiceContainerLabelListScrollbarTextComponent JComponentWindow Frame JFrame Dialog JDialog PanelScrollpane Applet JApplet java.awt.* javax.swing.* JLabelJListAbstractButton JButton JPanelJScrollpane
6
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 AWT vs. Swing AWT Abstract Windowing Toolkit –Original Java GUI toolkit –Lowest-common denominator for all Java host environments –Java standard, e.g., works even with old Browsers –Robust / thread-safe SWING –Flexible, powerful, but rather complex add-on –Richer set of GUI components –Pluggable Look-and-Feel Support –Internationalization –Not thread safe
7
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Swing Design Principles A graphical user interface is built as a containment hierarchy of GUI components Model Concept. Data displayed by a GUI component (so called model) are cleanly separated from their screen presentation (so called view) Events / Event Listeners / Event Objects. Defined Events that stem from human computer interaction (a window has been moved, the mouse has been clicked) trigger event listeners. Information about the triggering event is submitted to the event listener as an event object.
8
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 The Initial Swing GUI Containment Hierarchy aTopLevelContainer: JFrame or JDialog or JApplet rootPane: JRootPane (JPanel) glassPane: java.awt.Component (JPanel) contentPane: java.awt.Container layeredPane: JLayeredPane menuBar: JMenuBar optional
9
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 The Initial Swing GUI Containment Hierarchy File Edit Undo Redo Cut Frame / Dialog / Applet Root Pane Layered Pane Content Pane Glass Pane a 3D model enables menus to pop up above the content pane allows for interception of mouse events and painting across GUI components
10
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Concrete Example Containment Hierarchy JMenu fileMenu = new JMenu("File"); fileMenu.add("New"); fileMenu.add("Open"); fileMenu.add("Close"); JMenu editMenu = new JMenu("Edit"); editMenu.add("Undo"); editMenu.add("Redo"); editMenu.add("Cut"); JMenuBar mymenubar = new JMenuBar(); mymenubar.add(fileMenu); mymenubar.add(editMenu); JFrame myframe = new JFrame("My Frame"); myframe.setJMenuBar(mymenubar); File Edit Undo Redo Cut File Edit New Open Close
11
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Swing Components Top-Level Containers General-Purpose Containers Special-Purpose Containers Basic Controls Text Components Uneditable Information Displays Interactive Displays of Highly Formatted Information
12
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Top-Level Containers JFrame. Full GUI application JDialog. For the creation of customized Dialogs. For standardized dialogs use JOption pane instead. JApplet GUI components that are on the top of a containment hierarchy Top-level containers do not inherit from JComponent
13
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 General-Purpose Containers JPanel JScrollPane JSplitPane JTabbedPane JToolBar
14
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Special-Purpose Containers JInternalFrame JLayeredPane
15
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Basic Controls JButton JCheckbox JRadioButton and ButtonGroup
16
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Basic Controls Combobox List Menu Slider Spinner
17
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Text Components Text Controls: –JTextField –JPasswordField –JFormattedTextField Plain Text Areas: JTextArea Styled Text Areas: –JEditorPane –JTextPane
18
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Uneditable Information Displays JLabel JToolTip JProgressBar
19
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Interactive Displays of Highly Formatted Information JColorChooser JFileChooser JTree JTable
20
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Models of GUI Components Data displayed by a GUI component (so called model) are cleanly separated from their screen presentation (so called view) Origin of terminology: Model-View-Controller (Smalltalk) A GUI component can have several models. More precisely, a model can be: –Displayed data (e.g. list items - ListModel) –Information about state (selected item - ListSelectedModel) Advantages –Non-volatile concept for displayed data: no redundant data structures are necessary –The GUI model concept is integrated with the GUI event concept. Changes of the model trigger events and can therefore be observed in a well-defined way.
21
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Using Models - Example listModel = new DefaultListModel(); listModel.addElement("Alan Sommerer"); …. list = new JList(listModel); public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); listModel.remove(index); }
22
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Swing Event Concept event source event listener event object event a.k.a event handler a.k.a event processing
23
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Swing Event Concept aJButtonanActionListener anActionEvent button pressed Class ActionEvent extends AWTEvent { Object getSource() } Interface ActionListener extends EventListener { void actionPerformed(ActionEvent e) } public class SwingApplication implements ActionListener { public Component createComponents() { button.addActionListener(this); } public void actionPerformed(ActionEvent anActionEvent) { event processing code }
24
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Kinds of Events Low-level events versus semantic events Listeners supported by all Swing Components –Component listener –Focus listener –Key listener –Mouse listener –Mouse-motion listener –Mouse-wheel listener Component specific event listeners –Action –Caret –Change –Document, undoable edit –Item –List selection –Window
25
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Event Adapter Example: MouseListener versus Mouse Adapter void mouseClicked(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e)
26
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Swing and Threads Swing is not thread-safe There is one event-dispatching thread The single-thread rule: once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread. The responsiveness rule: Don’t do time-consuming tasks in event listeners. Spawn new threads for those instead. The responsiveness rule The single-thread rule
27
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Exceptions to the Single-Thread Rule A few methods are marked in the API as being thread- safe: This method is thread safe, although most Swing methods are not. No contradiction to the rule: construction before the realization: public static void main(String[] args) { JFrame f = new JFrame(); construction f.pack(); f.show(); no more construction !! }
28
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Swing Thread Safety Counterexample public static void main( String args[ ] ){ final DefaultListModel model = new DefaultListModel(); JList list = new JList( model ); JFrame frame = new JFrame(); frame.getContentPane().add( list ); frame.setVisible( true ); new Thread(){ public void run() { setPriority( Thread.MIN_PRIORITY ); while ( true ) model.addElement("FOO"); }}.start(); new Thread(){ public void run() { setPriority( Thread.MIN_PRIORITY ); while ( true ) model.removeElement("FOO"); }}.start(); }}
29
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Swing Thread Safety Counterexample Error message from the runtime system: Exception in thread “AWT-Event-Queue-0” java.lang.ArrayIndexOutOfBoundsException: 4145 >= 4145 at java.util.Vector.elementAt(Unkown Source) Code from the API implementation: public synchronized Object elementAt( int index ) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException( index + " >= " + elementCount); }
30
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Submitting Code to the Event-Dispatching Thread javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createAndShowGUI(); } });
31
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Directed Reading Using Swing Components –Using Top-Level Containers –Using Models –The JComponent Class –Using Text Components –Text Component Features –The Text Component API –How to Use Buttons, Check Boxes, and Radio Buttons –How to Make Dialogs –How to Use Labels –How to Use Lists –How to Use Panels –How to Use Tool Tips Using Other Swing Features –How to Use Borders –How to Use Threads Laying Out Components Within a Container –A Visual Guide to Layout Managers –Using Layout Managers –How Layout Management Works –How to Use GridBagLayout Writing Event Listeners –Introduction to Event Listeners –General Information about Writing Event Listeners –Listeners Supported by Swing Components –Implementing Listeners for Commonly Handled Events –How to Write an Action Listener –How to Write a List Data Listener –How to Write a Mouse Listener –How to Write a Mouse-Motion Listener –Listener API Table Performing Custom Painting –How Swing Components Are Displayed –Introduction to Painting Concepts –Implementing a Custom Component
32
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Summary / Next Topics Summary –Don’t ask what kind / Don’t call us, we’ll call you –Containment hierarchies of GUI components –Models of Swing GUI components –Swing GUI event concept –Swing and Threads –Directed Reading Next lab: Swing exercises. Next tutorial on 12 th May: –MS Visual Studio Designer –HTML WYSIWYG Editor –Basic Swing drawing –Game programming taster (timing) Next lecture: Programming Dynamic Web Pages Lab next week: guided tour to JSP technology
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.