THE SWING UI TOOLKIT Mostly from “The Swing Connection”The Swing Connection.

Slides:



Advertisements
Similar presentations
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Advertisements

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 program.
User Interface Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
COSC 3461: Module 8 Model-View-Controller (MVC) Architecture.
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.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng 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.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
CS3 - AWT/Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
GUI Programming in Java
W14 - Building World-Class UIs with JFC - Ted Faison1 Building World-Class User Interfaces with Java Foundation Classes Ted Faison Faison Computing Inc.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
Inheritance Abstract Classes Check out Inheritance from SVN.
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
Swing and MVCS CompSci 230 Software Construction.
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
3461 Model-View Controller Advanced GUI concepts.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
CSE1030-HR GUI The Big Picture Building the View Building the Controller Separating the Concerns Going Further.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Swing / Session1 / 1 of 30 Module Introduction Distributed Computing in Java.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
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.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
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.
The Swing GUI Components Chapter 29 An enhanced alternative to AWT The PC does not need an appletviewer or browser Swing overcomes some AWT drawbacks.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
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.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
1 Lecture 8: User Interface Components with Swing.
Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:
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:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
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
University of Central Florida COP 3330 Object Oriented Programming
Lecture 27 Creating Custom GUIs
University of Central Florida COP 3330 Object Oriented Programming
Swing & the JFC – Advanced Java GUI
A Quick Java Swing Tutorial
Event Driven Programming and Graphical User Interface
Building Graphical User Interface with Swing a short introduction
Week 8 Swing NetBeans GUI Builder
A Quick Java Swing Tutorial
Java 2 Swing B.Ramamurthy 5/1/2019 B.Ramamurthy.
Presentation transcript:

THE SWING UI TOOLKIT Mostly from “The Swing Connection”The Swing Connection

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing2 SWING TOOLKIT 100% Java implementation of components Pluggable Look & Feel –customizable for different environments, or –use Java L&F in every environment Lightweight components –no separate (child)windows for components –allows more variation on component structure –makes L&F possible Three parts –component set (subclasses of JComponent) –support classes –interfaces

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing3 OTHER APIs

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing4 UI COMPONENTS

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing5 BUTTONS

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing6 MENUS JMenuBar

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing7 OTHER COMPONENTS JApplet Border Interface JColorChooser JComboBox ImageIcon JDialog JFileChooser JInternalFrame

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing8 OTHER COMPONENTS JList JOptionPane JScrollBar JScrollPane JSlider JSplitPane JTabbedPane JLabel

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing9 OTHER COMPONENTS JTable JTextArea JTextField JToolBar JToolTip JTree

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing10 ARCHITECTURE Goals: –entirely on Java –pluggable L&F –model-driven programming –JavaBeans –compability with AWT Use MVC? –Model represents the data –View as a visual representation of the data –Controller takes input and translates it to changes in data

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing11 THE UI DELEGATE No reason to separate controller and view A separate UI object for defining the visual representation and controller behaviour Üthe UI delegate

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing12 MODELS Data-centric applications Separate model interface for every component –GUI-state models up-down state in JButton and subclasses –application data models selection state in JToggleButton and subclasses Application programmer can implement his/her own data models for existing components Shared model definitions

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing13 MODEL SEPARATION JSlider uses BoundedRangeModel –public JSlider(int orientation, int min, int max, int value) { checkOrientation(orientation); this.orientation = orientation; this.model = new DefaultBoundedRangeModel(value, 0, min, max); this.model.addChangeListener(changeListener); updateUI(); } Calling setModel, application can replace the default –JSlider slider = new JSlider(); BoundedRangeModel myModel = new DefaultBoundedRangeModel() { public void setValue(int n) { System.out.println("SetValue: "+ n); super.setValue(n); } }); slider.setModel(myModel);

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing14 CHANGE NOTIFICATION Models implement methods for adding and removing listeners Lightweight notification –only notify –listener queries about the changes –e.g. scrollabar dragged Stateful notification –event described the change –for complex data models –e.g. changes in the column of table

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing15 LIGHTWEIGHT NOTIFICATION ChangeListener with one single method –public void stateChanged(ChangeEvent e); Listening to JSlider –JSlider slider = new JSlider(); BoundedRangeModel model = slider.getModel(); model.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { // need to query the model to get updated value... BoundedRangeModel m = (BoundedRangeModel)e.getSource(); System.out.println("model changed: " + m.getValue()); } });

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing16 STATEFUL NOTIFICATION Tracking JList selection –String items[] = {"One", "Two", "Three"); JList list = new JList(items); ListSelectionModel sModel = list.getSelectionModel(); sModel.addListSelectionListener (new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { // get change information directly // // from the event instance... if (!e.getValueIsAdjusting()) { System.out.println("selection changed: " + e.getFirstIndex()); } } });

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing17 IGNORING MODELS Most components provide API to the model directly E.g. JSlider’s method –public int getValue() { return getModel().getValue(); } Program can simply do the following –JSlider slider = new JSlider(); int value = slider.getValue(); So, where’s the “model,” anyway!

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing18 SETTING LOOK & FEEL To set a particular L&F (here CDE/Motif), write –UIManager.setLookAndFeel( "com.sun.java.swing.plaf.motif.MotifLookAndFeel” ); To set the appropriate L&F, whatever the current environment, write –UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); Do the above preferably at the end of the program (before instantiating any components)

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing19 THE SWING PACKAGES The Accessibility package (javax.accessibility) –provides support for supporting the screen access products for people with disabilities –Swing has full support for accessibility javax.swing –contains nearly all of the Swing components –notable exception is JTextComponent (in javax.swing.text) javax.swing.border –in need for customized borders, take a look javax.swing.event –includes the additional event classes (not found in java.awt.event)

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing20 THE SWING PACKAGES (cont’d) javax.swing.plaf –classes for providing the L&F capabilities –also javax.swing.plaf.basic including the default L&F classes –the current specialized L&F:s javax.swing.plaf.metal javax.swing.plaf.motif (or com.sun.java.swing.plaf.motif) javax.swing.plaf.windows (or com.sun.java.swing.plaf.windows) –also javax.swing.plaf.multi for mixing multiple L&F:s javax.swing.table –including support classes for managing tables javax.swing.tree –support classes for managing trees

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing21 THE SWING PACKAGES (cont’d) javax.swing.text –support classes for text editing –Document classes –JTextComponent (superclass for all text components) –see also separate format packages javax.swing.text.html javax.swing.text.rtf javax.swing.undo –classes for supporting undo/redo operations

PROGRAMMING GRAPHICAL USER INTERFACES Lecture 4: Swing22 JComponent An abstract root class of almost all of Swing components Provides –pluggable L&F –extensibility –smart trapping of keyboard events (see javax.swing.KeyStroke) –customizable borders –easy resizing –tool tips –autoscrolling –support for debugging –support for accessibility –support for localization java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent