Swing Advanced HCI (IAT 351)

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
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.
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.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
©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.
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.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
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
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Welcome to CIS 083 ! Events CIS 068.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Lecture 18 – Swing Introduction Lecturer: Prof Jim Warren.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– 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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Layout Managers Arranges and lays out the GUI components on a container.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Java Swing Reference: 1.Swing: A Beginner’s Guide by Herbt Schildt, TMH Edition.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Software Design 5.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, but easy to use and hide.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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,
Java Swing One of the most important features of Java is its ability to draw graphics.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI Programming in Java Hao Jiang Boston College April, 2009.
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.
GUI 1: JFC and Swing Basics OOP tirgul No
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Welcome To java
Aum Amriteshwaryai Namah
Java Swing.
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Introduction to Computing Using Java
GUI building with the AWT
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Swing Advanced HCI (IAT 351) TA: Syavash Nobarany

Swing & AWT AWT: Abstract Windowing Toolkit Swing: new with Java2 import java.awt.* Swing: new with Java2 import javax.swing.* Extends AWT Tons o’ new improved components

Partial AWT and Swing Class Hierarchy java.lang.Object CheckboxGroup Component MenuComponent Button Canvas Checkbox Choice Container Label List Scrollbar TextComponent JComponent Scrollpane Panel Window AbstractButton JLabel JList JPanel JScrollpane Applet Dialog Frame JButton JApplet JDialog JFrame java.awt.* javax.swing.*

Swing Features Pluggable look and feel Accessibility API: for people with disabilities Undo Framework API: supports unlimited numbers of actions to undo and redo Flexible Deployment: run within a browser as an applet or Java Web Start CDE/Motif Windows Metal

Swing Set Demo J2sdk/demo/jfc/SwingSet3

GUI Component API Java: GUI component = class Properties Methods Events JButton

Using a GUI Component Create it Configure it Add it Listen to it Instantiate object: b = new JButton(“press me”); Configure it Properties: b.text = “press me”; [avoided in java] Methods: b.setText(“press me”); Add it panel.add(b); Listen to it Events: Listeners JButton

Event-Driven UIs Old model (e.g., UNIX shell, DOS) Interaction controlled by system, user queried for input when needed by system Event-Driven Interfaces (e.g., GUIs) Interaction controlled by user System waits for user actions and then reacts More complicated programming and architecture

Event Dispatch Loop Event Queue Event Loop (runs in dedicated thread) Queue of input events Event Loop (runs in dedicated thread) Remove next event from queue Determine event type Find proper component(s) Invoke callbacks on components Repeat, or wait until event arrives Mouse moved (t0,x,y) Component Invoked callback method Update application state Request repaint, if needed

Event Dispatch Window Panel Label TextArea Panel Button Button Event Queue Mouse moved (t0,x,y) Mouse pressed (t1,x,y,1) Mouse dragged (t2,x,y,1) Key typed (t3, ‘F1’) … (queues and dispatches incoming events in a dedicated thread) Window Panel Label TextArea Panel /* callback for TextArea */ public void mouseMoved(e) { // process mouse moved event } Button Button

Events, Listeners, Adapters and Handler Methods Listener / Adapter Handler Methods ActionEvent ActionListener actionPerformed AdjustmentEvent AdjustmentListener adjustmentValueChanged MouseEvent MouseListener MouseAdapter mouseClicked mouseEntered mouseExited mousePressed mouseReleased KeyEvent KeyListener KeyAdapter keyPressed keyReleased keyTyped ComponentEvent ComponentListener ComponentAdapter componentShown componentHidden componentMoved componentResized Adapter classes with empty methods for Listener interfaces with >1 methods

Anatomy of an Application GUI Internal structure GUI JFrame JFrame JPanel containers JPanel JButton JButton JLabel JLabel

Using a GUI Component Create it Configure it Add children (if container) Add to parent (if not JFrame) Listen to it order important

Build from bottom up Create: Add: (bottom up) Frame Panel Components Listener Create: Frame Panel Components Listeners Add: (bottom up) listeners into components components into panel panel into frame JButton JLabel JPanel JFrame

How does HelloWorld look like? class HelloButtonListener implements ActionListener { private JButton button; HelloButtonListener(JButton b){ button = b; } public void actionPerformed(ActionEvent e){ button.setText("Hello World!"); import java.awt.event.*; import javax.swing.*; public class HelloAgain extends JFrame { HelloAgain(){ super("Example: Swing GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton b = new JButton("Show message!"); b.addActionListener(new HelloButtonListener(b)); add(b); setSize(250,100); } public static void main(String[] args){ new HelloAgain().setVisible(true); ActionEvent Mouse click

Keyword “super” A method that overrides a superclass's method Invoke the overridden method using “super” Example: public class Library extends AbstractLibrary { @Override public void printAddress() { super.printAddress(); // calls AbstractLibrary’s printAddress System.out.println(address); } …

Layout Managers A layout manager helps in arranging the components in a container Each layout manager: Encapsulates an algorithm for positioning and sizing of components Automatically calculates the coordinates of each component it manages If a container is resized, the layout manager readjusts the placement of the components

Layout Manager Heuristics null FlowLayout none, programmer sets x,y,w,h Left to right, Top to bottom BorderLayout GridLayout n c w e s

null layout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); b.setBounds(new Rectangle(10,10, 100,50)); p.setLayout(null); // x,y layout p.add(b); f.setContentPane(p); press me

BorderLayout Allows placing of components by using the geographic terms: CENTER EAST NORTH SOUTH WEST The components are placed around the edges The component in the center uses the remaining space import java.awt.*; import java.swing.*; import java.awt.event.*; class BorderExample extends JFrame { BorderExample() { super("Example: BorderLayout"); setLayout(new BorderLayout()); add(new Button("Center"),BorderLayout.CENTER); add(new Button("East"),BorderLayout.EAST); add(new Button("North"),BorderLayout.NORTH); add(new Button("South"),BorderLayout.SOUTH); add(new Button("West"),BorderLayout.WEST); setSize(200,200); } public static void main(String[] args) new BorderExample().setVisible(true);

GridLayout Automatically arranges components in a grid of rows and columns The container is divided into equal-sized cells, and one component is placed in each cell import java.awt.*; import java.swing.*; import java.awt.event.*; class GridExample extends JFrame { GridExample() { super("Example: GridLayout"); setLayout(new GridLayout(2,2)); add(new Button("1,1")); add(new Button("1,2")); add(new Button("2,1")); add(new Button("2,2")); setSize(250,100); } public static void main(String[] args) new GridExample().setVisible(true);

FlowLayout Set layout mgr before adding components press me then me JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); FlowLayout L = new FlowLayout( ); JButton b1 = new JButton(“press me”); JButton b2 = new JButton(“then me”); p.setLayout(L); p.add(b1); p.add(b2); Set layout mgr before adding components press me then me

Combinations JButton JButton JTextArea

Combinations JButton JButton JFrame n JPanel: BorderLayout c JPanel: FlowLayout JTextArea

Menu, MenuBar and MenuItem import java.awt.*; import java.awt.event.*; import java.swing.*; class MenuExample extends JFrame { MenuExample(){ super("Example: MenuBar"); final MenuBar mb = new MenuBar(); setMenuBar(mb); final Menu m = new Menu("File"); MenuItem mi; mi = new MenuItem("Exit"); mi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ System.exit(0); } }); m.add(mi); mb.add(m); setSize(250,100); public static void main(String[] args){ new MenuExample().setVisible(true); A frame may contain a menu bar with options When the mouse is clicked on an option a drop down menu appears Each menu consists of one or more menu items

JOptionPane Examples

JPanel Panel is the simplest container class import java.swing.*; import java.awt.event.*; class PanelExample extends JFrame { PanelExample(){ super("Example: Panel"); final Panel p = new JPanel(); p.add(new JButton("1")); p.add(new JButton("2")); p.add(new JButton("3")); add(p); setSize(250,100); } public static void main(String[] args){ new PanelExample().setVisible(true); Panel is the simplest container class A panel provides space in which an application can attach any other component, including other panels The default layout manager for a panel is the FlowLayout manager

Swing Architecture 2 important cases Features of a good system architecture: Easy to Understand Change Debug Reuse Design Principle: Reduced coupling 2 important cases View form Logic/Model Model from Database

Separating View from Logic/Model Model-View-Controller pattern Provides for a good way to partition work and create a modular design MVC components Model Data storage, no presentation elements View No data storage, presentation elements Controller Glue to tie the model and the view together

Modified MVC in Swing JList contains the visual properties ListModel: Contains the data in the list JList UI Manager List Model List UI

Applets JApplet is like a JFrame Already has a panel Access panel with JApplet.getContentPane( ) import javax.swing.*; class hello extends JApplet { public void init(){ JButton b = new JButton(“press me”); getContentPane().add(b); } contentPane JButton

Applet Methods Called by browser: init( ) - initialization start( ) - resume processing (e.g. animations) stop( ) - pause destroy( ) - cleanup paint( ) - redraw stuff (‘expose’ event)

Application + Applet Command line Browser JFrame or JApplet import javax.swing.*; class helloApp { public static void main(String[] args){ // create Frame and put my mainPanel in it JFrame f = new JFrame(“title”); mainPanel p = new mainPanel(); f.setContentPane(p); f.show(); } class helloApplet extends JApplet { public void init(){ // put my mainPanel in the Applet getContentPane().add(p); // my main GUI is in here: class mainPanel extends JPanel { mainPanel(){ setLayout(new FlowLayout()); JButton b = new JButton(“press me”); add(b); Command line Browser JFrame or JApplet contentPane JPanel JButton

Thank you