Lecture 18 – Swing Introduction Lecturer: Prof Jim Warren.

Slides:



Advertisements
Similar presentations
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Advertisements

Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
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.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
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.
Graphical User Interface (GUI) Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
GUI Programming in Java
Welcome to CIS 083 ! Events CIS 068.
CompSci 230 S Software Construction Frameworks & GUI Programming.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
YEAR 2006 The University of Auckland | New Zealand SOFTENG 350 Programming Object-Oriented GUIs II Swing Components and Events SOFTENG 350 – Lecture 17.
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.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphical User Interface Components: Part 1 Chapter 11.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
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.
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.
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
Object Oriented Programming.  Interface  Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
5-Jan-16 R Ramesh Swing. 5-Jan-16 R Ramesh An Introduction to Java Foundation Classes (JFC) A suite of libraries to assist programmers create enterprise.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
1 Lecture 8: User Interface Components with Swing.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
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:
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
Java Swing What is swing? Swing is a ”Lightweight” GUI ToolKit for Java Different from AWT / SWT Toolkits for GUIs Few lines of code to produce GUI elements.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
CompSci 230 S Software Construction
Advanced User Interfaces
A Quick Java Swing Tutorial
Swing Advanced HCI (IAT 351)
Introduction to Computing Using Java
Graphical User Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
Events, Event Handlers, and Threads
Presentation transcript:

Lecture 18 – Swing Introduction Lecturer: Prof Jim Warren

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

3 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

4 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 Windowing System Application 1 Application 2 Mouse up Draw circle Mouse Keyboard Mouse up

5 GUI Interaction Events Primitive Pointer Events Mouse Moved Mouse Down Mouse 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 Primitive Keyboard Events Key Down Key Up

6 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 Application 1 Button 1 Activated Mouse up Mouse down Paint

7 Rendering of Widgets 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) Button Activate green window Update Region

8 The GUI Event Loop 1.GUI application is started 2.Widgets are set up 3.Event loop is started 4.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

9 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

10 Introduction to Java Swing

11 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

12 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

13 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

14 Swing Widgets (more on this next lecture) Top-Level Containers JFrame JPanel JSplitPane JTabbedPane JDialog JScrollPane General-Purpose Containers JButton JCheckbox JRadioButton and ButtonGroup JCombobox JLabel JList Menu

15 More Swing Widgets JColorChooser JFileChooser JTree JTable

16 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

17 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

18 Swing Hello World import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HelloWorld { public static void main(String[] args) { JFrame frame = new JFrame("Hello World!"); frame.setSize(220, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(null); JButton button = new JButton("Hello World!"); button.setLocation(30, 30); button.setSize(150, 100); contentPane.add(button); frame.setVisible(true); }

19 Swing Hello World with Events import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { Toolkit.getDefaultToolkit().beep(); }... public class HelloWorld { public static void main(String[] args) {... JButton button = new JButton("Hello World!"); button.addActionListener(new MyActionListener());... }

20 Containment Hierarchy of a Menu … public class MenuExample { public static void main(String[] args) { JFrame frame = new JFrame("My Frame"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); JMenu fileMenu = new JMenu("File"); fileMenu.add(new JMenuItem("New")); fileMenu.add(new JMenuItem("Open")); fileMenu.add(new JMenuItem("Close")); JMenu editMenu = new JMenu("Edit"); editMenu.add(new JMenuItem("Undo")); editMenu.add(new JMenuItem("Redo")); editMenu.add(new JMenuItem("Cut")); JMenuBar menubar = new JMenuBar(); menubar.add(fileMenu); menubar.add(editMenu); frame.setJMenuBar(menubar); frame.setVisible(true); } File Edit Undo Redo Cut File Edit New Open Close

21 Handling Menu Events... public class MenuActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(MenuExample.frame, "Got an ActionEvent at " + new Date(e.getWhen()) + " from " + e.getSource().getClass()); }... public class MenuExample { static JFrame frame; public static void main(String[] args) {... JMenuItem item = new JMenuItem("Close"); item.addActionListener(new MenuActionListener()); fileMenu.add(item);... }

22 Defining Event Listeners with Anonynous Classes 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)... public class MenuExample { public static void main(String[] args) { final JFrame frame = new JFrame("My Frame");... JMenuItem item = new JMenuItem("Close"); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int n = JOptionPane.showOptionDialog(frame,... } });... }

23 Different Kinds of Swing Events Low-level events MouseEvent : Component got mouse-down, mouse-move, etc. KeyEvent : Component got key-press, key-release, etc. ComponentEvent : Component resized, moved, etc. ContainerEvent : Container's contents changed because a component was added or removed FocusEvent : Component got focus or lost focus WindowEvent : Window opened, closed, etc. High-level semantic events ActionEvent : Main action of control invoked (e.g. JButton click) AdjustmentEvent : Value was adjusted (e.g. JScrollBar moved) ItemEvent : Item was selected or deselected (e.g. in JList) TextEvent : Text in component has changed (e.g in JTextField)

24 Events, Listeners, Adapters and Handler Methods EventListener / AdapterHandler Methods ActionEventActionListeneractionPerformed AdjustmentEventAdjustmentListeneradjustmentValueChanged MouseEventMouseListener MouseAdapter mouseClicked mouseEntered mouseExited mousePressed mouseReleased KeyEventKeyListener KeyAdapter keyPressed keyReleased keyTyped ComponentEventComponentListener ComponentAdapter componentShown componentHidden componentMoved componentResized Adapter classes with empty methods for Listener interfaces with >1 methods

25 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: