Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Graphic User Interfaces Layout Managers Event Handling.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Object-Oriented Analysis and Design
Graphical User Interface (GUI) Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
GUI Programming in Java
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
CSE1030-HR GUI The Big Picture Building the View Building the Controller Separating the Concerns Going Further.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Review_6 AWT, Swing, ActionListener, and Graphics.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
A Quick Java Swing Tutorial
Graphical User Interfaces
Provision for GUIs in Java
A First Look at GUI Applications
Review: Java GUI Programming
Provision for GUIs in Java
Lecture 8 Object Oriented Programming Using Java
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Event Driven Programming and Graphical User Interface
Building Graphical User Interface with Swing a short introduction
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science University of Alberta

2 Java Foundation Classes JFC: –Abstract Window Toolkit (AWT) –original user interface toolkit –don’t go there! –Swing –package javax.swing.*, introduced in Java 1.2

3 Swing Portable API: –The appearance and behavior (look-and- feel) of the user interface components are implemented in Java … –might work slightly differently from any host platform –pluggable look-and-feels e.g., Motif, windows,…

4 Containment 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, JTabbedPane

5 Containment Hierarchy Atomic components: –self-sufficient components that present information to and get input from the user –e.g., JButton, JLabel, JComboBox, JTextField, JTable

6 Swing Components and containers: –superclasses and interfaces –extends and implements © O’Reilly 1999

7 Swing Java Documentation: – 1/docs/api/javax/swing/pack age-summary.html SwingSet: – /javawebstart/demos.html Quick tutorial: – oks/tutorial/uiswing/start/swi ngTour.html

8 Containers Notes: –Container objects group components, arranging them for display with a layout manager.

9 Top-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 )

10 Events Two approaches to event handling –read-evaluation loop (client-written loop) –notification-based (callbacks) Swing uses the 2nd approach

11 Events 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 objects

12 Events Swing: –different event sources produce different kinds of events e.g., a JButton object, when clicked, generates an ActionEvent object, which is handled by an ActionListener (an object whose class implements this interface)

13 Events

14 Events 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()

15 Event Handling class MyListener implements ActionListener { … public void actionPerformed( ActionEvent event ) { // react to event … } } … // instantiate event listener ActionListener listener = new MyListener(); … // instantiate event source JButton button = new JButton( “Hello” ); … // register event listener with event source button.addActionListener( listener );

16 UML Sequence Diagram

17 Event Handling Options for implementing listeners: –listener class –anonymous inner classes –named inner classes

18 Event Handling Listener class:

19 Event Handling Anonymous inner listener class:

20 Event Handling Named inner listener class:

21 Event Handling Note: –A class could potentially be both an event source and event listener. –Good or bad idea? …

22 Event Handling public class MyButton extends JButton implements ActionListener { … public MyButton() { … addActionListener( this ); } … public void actionPerformed( ActionEvent event ) { … } } JButton button = new MyButton() …

23 Dependencies

24 Dependencies 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, persistence

25 Model/View/Controller MVC roles: –model –complete, self-contained representation of object managed by the application e.g., spreadsheet document –provides a number of services to manipulate the data e.g., recalculate, save –computation and persistence issues –…

26 Model/View/Controller MVC roles: –view –tracks what is needed for a particular perspective of the data e.g., bar chart view –presentation issues –controller –gets input from the user, and uses appropriate information from the view to modify the model e.g., get slider value, trigger chart modify –interaction issues

27 Model/View/Controller

28 Model/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?

29 Model/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 APIs

30 Model/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 application

31 Model/View/Controller Smalltalk: –originated the MVC concept –integral support in interactive applications with MVC classes

32 Model/View/Controller Java and Swing: –concept is still valid to help structure interactive applications e.g., use a framework that supports MVC –Swing internally uses a variant of MVC for its pluggable look-and-feel capability …

33 Pluggable 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” );

34 Pluggable Look-and-Feel SwingSet: –

35 Pluggable Look-and-Feel Idea: –similar to skins, themes, schemes, etc., but must include “feel” as well as “look”

36 Pluggable Look-and-Feel Swing internals: –each component uses a user interface delegate object (responsible for view and controller roles)

37 Pluggable Look-and-Feel Swing internals: –each component specifies a model interface that an associated model class must implement

38 Model/View/Controller CRC cards for MVC: –discuss what models, views, and controllers there are in the system –be a design critic