Java: Graphical User Interfaces (GUI)

Similar documents
Graphical interfaces & event-driven programming

Introduction to the JAVA UI classes Advanced HCI IAT351

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Jonathan Aldrich Charlie Garrod

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Graphical User Interface (GUI)

2010 가을학기부산대학교정보컴퓨터공학부 OVERVIEW OF GUI PROGRAMMING

CS 251 Intermediate Programming GUIs: Components and Layout

Graphical User Interfaces (GUIs)

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT

Event Driven Programming

Chapter 6: Graphical User Interfaces

COMPSCI 230. Software Design and Construction. Swing

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Packages: Putting Classes Together

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Chapter 12 GUI Basics

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

GUI Software Architecture

Introduction to concurrency and GUIs

Programming graphics

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Graphical User Interface (GUI)

Dr. Hikmat A. M. AbdelJaber

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Java Graphical User Interfaces

CS 251 Intermediate Programming GUIs: Event Listeners

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

PIC 20A GUI with swing

Graphical User Interfaces

Window Interfaces Using Swing Objects

UI Software Organization

Java IDE Programming-I

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

(Incomplete) History of GUIs

User interfaces and Swing

CS11 Java. Fall Lecture 3

Graphical User Interface (GUI)

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Lecture 5: Java Graphics

Part 3: Graphical User Interface (GUI) & Java Applets

Systems Programming Graphical User Interfaces

Part I: Learn Common Graphics Components

Chris9an Kästner Charlie Garrod

Swing. By Iqtidar Ali

Is image everything?

Lecture 18 Java Graphics and GUIs

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

Graphical User Interfaces in Java - SWING

CS11 Java. Fall Lecture 4

Basicsof. JavaGUI and SWING

INTRODUCTION TO (GUIS)

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Window Interfaces Using Swing Objects

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Sri Vidya College of Engineering & Technology

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Lecture 9. Lecture

CSE 331 Software Design & Implementation

Charlie Garrod Bogdan Vasilescu

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Charlie Garrod Michael Hilton

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Programmierpraktikum

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

CSE 331 Software Design & Implementation

Graphical User Interfaces. Comp 152

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

SD Module-1 Advanced JAVA

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Supporting Materials

SD Module-1 Advanced JAVA. Assignment No. 4

AP CS Unit 11: Graphics and Events

Building Graphical User Interfaces. GUI Principles

Transcription:

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, and Bertrand Meyer Java: Graphical User Interfaces (GUI) With material from Christoph Angerer

The essence of the Java Graphics API Application Swing AWT 2D JVM 2

Application Swing AWT 2D JVM 3

Abstract Windowing Toolkit (AWT) The first available API for Java GUIs Platform independent (if there are JVMs) It does not look the same on all platforms Calls native libraries on the user system Handles events, cut and paste, drag and drop, keyboard focus, user input Still used directly for top-level containers (frames, applets, dialogs) 4

AWT Components 5

Application Swing AWT 2D JVM 6

Introduced in 1.2 Java 2D Basic and advanced drawing operations Image manipulation Text Printing Can be used directly, or indirectly via Swing Handles Swing s rendering operations (e.g. GUI component background and border) 7

The essence of the Java Graphics API Application Swing AWT 2D JVM 8

Swing and AWT 9

Introduced in 1.2 Java Swing Main Java GUI library for desktop apps Lightweight relies on 2D for graphics relies on AWT for top-level containers and application behavior via event management 10

UI structure Swing Concepts Top-level containers (need native support): JFrame, JDialog, JApplet. N-level containers (implemented in Java): JPanel, JSplitPane, JTabbedPane,... Leaf components: JButton, JTextField, JTable, JList, JProgressBar, JScrollBar,... UI design via layout managers 11

Top-level containers structure: JRootPane Top-level containers always contain a JRootPane JRootPane contains JLayeredPane, a content pane, JMenuBar, and a glass pane Can be used to intercept mouse clicks and paint over multiple components 12

Top-level containers structure: JLayeredPane JRootPane contains a JLayeredPane JLayeredPane contains and positions a content pane, an optional menu bar, and possibly dialogs and toolbars Enables Z-ordering of the contained components 13

Top-level containers structure: content pane In the content pane go your visible GUI components JPanel is the typical content pane First create a JPanel, then add components to it, and then: mytoplevelcontainer.setcontentpane(myjpanel) 14

Top-level containers structure: glass pane The glass pane sits on top of everything, fills the entire view, and it s by default invisible. Used to intercept mouse and keyboard clicks, drag & drop, and to draw over the entire UI, especially if there are already other components 15

Creating a JFrame window public class MyFrame extends JFrame { private JPanel contentpane; public MyFrame(){ setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 450, 300); contentpane = new JPanel(); setcontentpane(contentpane); } } 16

Displaying a JFrame window public class MyFrame extends JFrame { public static void main(string[] args){ EventQueue.invokeLater(new Runnable(){ public void run(){ try{ MyFrame frame = new MyFrame(); frame.setvisible(true); } catch (Exception e) {...} }}); }...} 17

Setting up a glass pane public class MyGlassPane extends JComponent { @Override protected void paintcomponent(graphics g){ Rectangle clip = g.getclipbounds(); g.setcolor(color.blue); g.fillrect(clip.x, clip.y, clip.width, clip.height); } } 18

Adding a glass pane on top of a JFrame public class MyFrame extends JFrame { private JPanel contentpane; public MyFrame(){ setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 450, 300); contentpane = new JPanel(); setcontentpane(contentpane); setglasspane(new MyGlassPane()); getglasspane().setvisible(true); }...} 19

Before and after applying the glass pane 20

Threading Concepts Threads running in a Swing application Main thread: runs the application s main method Toolkit thread: captures system events (mouse, keyboard, ) Event Dispatching Thread (EDT): responsible for executing any method that modifies a component s state dispatches the events captured by the toolkit thread to the appropriate components the recommended way to interact with Swing 21

Caveats Swing is not a thread-safe API. It is a single-threaded API, and that thread is the EDT Swing should be used only on the EDT (see previous example) Hint: don t perform long-lasting computations or I/O accesses in a method executed by the EDT Where do you perform these computations then? 22

Handling heavy computations and I/O SwingUtilities.invokeLater(new Runnable(){ public void run(){ // lengthy computation }}); This posts a new task on the EDT by invoking EventQueue.invokeLater(...) From Java 6 you can use SwingWorker, a utility class to run a task on a background thread, and post intermediate or final results on the EDT 23

SwingWorker usage For running long-running tasks in a different thread so as to prevent the GUI from being unresponsive For updating GUI with the results produced by the long-running task at the end of the task through method done() For updating GUI from time to time with the intermediate results produced and published by the task through methods process() and publish() 24

Observer design pattern Handling events Components maintain a list of objects implementing listener interfaces (listeners) You can register/unregister a listener XYZ on a component c: c.addxyzlistener or c.removexyzlistener When the component fires an event, all registered listeners are notified using a callback The reaction code is typically in the (anonymous inner) class implementing the listener interface 25

Adding a button and an associated action public class MyFrame extends JFrame { private JButton mybutton; public MyFrame(){... mybutton = new JButton( Push me! ); contentpane.setlayout(new FlowLayout()); mybutton.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ mybutton.settext("works! ); }}); contentpane.add(mybutton); setcontentpane(contentpane); }...} 26

Firing an ActionEvent 27

Some Listeners and related Components Event Listener Listener methods Register on ActionListener actionperformed() JButton, JComboBox, JFileChooser, MenuItem, JTextField, FocusListener MouseListener MouseMotionLi stener focusgained(), focuslost() mouseclicked(), mouseentered(), mouseexited(), mousepressed(), mousereleased() mousedragged(), mousemoved() Component Component Component 28

Some more Listeners and Components Event Listener Listener methods Register on KeyListener TextListener keypressed(), keyreleased(), KeyTyped() Component textvaluechanged() TextComponent CaretListener caretupdate() JTextComponent MenuListener menucanceled(), menudeselected(), menuselected() JMenu 29

Layout Managers Used to harmonize component placement They typically decide the component size Can be composed with one another React in a predictable way when adding/removing components and resizing the application window Absolute positioning (x, y, size) is still possible: contentpane.setlayout(null); 30

Some Layout Managers Border Layout: 5 areas BoxLayout: single row/column GridLayout: cells are same size FlowLayout http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 31

Some more Layout Managers CardLayout: different components at different times GridBagLayout: cells of different size SpringLayout: fine-grained control http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 32

Do it yourself You can experiment with the Java GUI APIs trying the RPN calculator assignment A nice GUI designer tool that produces clean GUI code in the background and you may want to have a look at is Google s Window Builder (Eclipse plug-in): https://developers.google.com/java-dev-tools/wbpro/ Or you can experiment with JavaFX (see next page) 33

An alternative to Swing: JavaFX Interoperable with Swing CSS for separating style from implementation Option to use FXML scripting language to separate UI from back-end logic JavaFX Scene Builder to create UI visually Embed web pages within a JavaFX app (WebView) and much more http://docs.oracle.com/javafx/2/overview/jfxpuboverview.htm 34