Introduction to the JAVA UI classes Advanced HCI IAT351

Similar documents
CS 251 Intermediate Programming GUIs: Components and Layout

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

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

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

Graphical User Interface (GUI)

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

Packages: Putting Classes Together

encompass a group of features for building Graphical User Interfaces (GUI).

Java: Graphical User Interfaces (GUI)

Swing. By Iqtidar Ali

CS11 Java. Fall Lecture 4

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

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.

Lecture 18 Java Graphics and GUIs

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

Graphical User Interfaces. Comp 152

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

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

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

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

Sri Vidya College of Engineering & Technology

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

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!

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

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Window Interfaces Using Swing Objects

Lecture 5: Java Graphics

CSE 331 Software Design & Implementation

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

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

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

Java Swing Introduction

BASICS OF GRAPHICAL APPS

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

CS11 Java. Fall Lecture 3

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

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Graphical User Interfaces. Swing. Jose Jesus García Rueda

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

Graphical User Interface (GUI)

Systems Programming Graphical User Interfaces

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

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

1.00 Lecture 14. Lecture Preview

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Class 14: Introduction to the Swing Toolkit

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphical interfaces & event-driven programming

1. Swing Note: Most of the stuff stolen from or from the jdk documentation. Most programs are modified or written by me. This section explains the

INTRODUCTION TO (GUIS)

Java IDE Programming-I

User interfaces and Swing

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Computer Science 210: Data Structures. Intro to Java Graphics

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Window Interfaces Using Swing Objects

Graphical User Interfaces (GUIs)

Building Graphical User Interfaces. GUI Principles

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Chapter 6: Graphical User Interfaces

Chapter 12 Advanced GUIs and Graphics

PIC 20A GUI with swing

Building Graphical User Interfaces. Overview

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Graphical User Interfaces 2

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

Unit 6: Graphical User Interface

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

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

Swing from A to Z Using Focus in Swing, Part 2. Preface

Lecture 9. Lecture

H212 Introduction to Software Systems Honors

(Incomplete) History of GUIs

Graphical User Interface (GUI)

Java Programming Lecture 6

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

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

Java. GUI building with the AWT

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

DM503 Programming B. Peter Schneider-Kamp.

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Dr. Hikmat A. M. AbdelJaber

Programming graphics

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

Chapter 12 GUI Basics

Part I: Learn Common Graphics Components

Laying Out Components. What is Widget Layout?

Java Graphical User Interfaces

Event Driven Programming

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

Programmierpraktikum

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Transcription:

Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca

About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing graphical user interfaces (GUI). Implemented without any native code. Swing is codename for lightweight JFC. AWT (heavyweight), Swing (lightweight)

Heavyweight and lightweight components Platform independence Swing AWT Performance Side effects! Some extra work down the road to control timing of events Advanced HCI IAT351 9.05.2012 3

About JFC and Swing Swing features: The Swing Components Dialog, Tabbed pane, Buttons, File Chooser,... Pluggable Look and Feel Accessibility API Screen readers, Braille displays,... Java 2D TM API Drag and Drop Between Java applications and native applications.

How to Learn Swing Don t even try. Learn general framework principles and design styles. Then use the API reference, and Swing Tutorials to discover detailed usage of each component. Java v7 API Reference available at: http://docs.oracle.com/javase/7/docs/api/ LOTS more resources on parent site

How to read Java Docs (2) General idea is find class, and examine main frame for information. Main frame pages split into sections: Package hierarchy & implemented interfaces Class Description, and links to more info Nested Class Summary Detail in separate page Fields - 2 types Class (static) and instance, plus fields inherited from parent classes / interfaces Constructor Summary Method Summary & inherited methods from parents Detailed info on all summary sections

Swing Elements many standard GUI components such as buttons, lists, menus, and text areas, which you combine to create your program's GUI. Swing provides containers such as windows and tool bars. top level: frames, dialogs intermediate level: panel, scroll pane, tabbed pane,... other Swing components: buttons, labels,... A visual index of Swing components

JSlider JButton JMenu JColorChooser JOptionPane JTabbedPane Advanced HCI IAT351 9.05.2012 8

Pluggable Look and Feel same program, different look and feel

Containers Descendents of the java.awt.container class can contain other components. Use a layout manager to position and size the components contained in them. Components are added to a Container using one of the various forms of its add method layout managers panel.add(component); All Containers are Components All Components are not COntainers

Top Level Containers Every program that presents a Swing GUI contains at least one top-level container. provides the support to perform painting and event-handling. three top-level containers: JFrame (Main window) JDialog (Secondary window) JApplet (An applet display area within a browser window)

Top Level Containers To appear on screen, every GUI component must be part of a containment hierarchy, with a top-level container as its root. Each top-level container has a content pane that contains visible components in that top-level container s GUI. Don t add a component directly to a top-level container.

JFrame A frame implemented as an instance of the JFrame class is a window that has decorations such as a border, a title and buttons for closing and iconifying the window. The decorations on a frame are platform dependent. Applications with a GUI typically use at least one frame.

Example 1 import javax.swing.*; public class MainUI { public static void main(string[] args) { } JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World"); frame.getcontentpane().add(label); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setvisible(true); } pack() causes a window to be sized to fit the preferred size and layouts of its sub-components Can t add to the JFrame directly; add to its default contentpane() Need to tell the program to close after the window (JFrame) is closed

Example 2 In this example a custom frame is created import javax.swing.*; public class MainUI extends JFrame { public MainUI() { super( HelloWorldSwing ); final JLabel label = new JLabel("Hello World"); getcontentpane().add(label); setdefaultcloseoperation(jframe.exit_on_close); pack(); setvisible(true); } public static void main(string[] args) { MainUI frame = new MainUI(); } }

JDialog Every dialog is dependent on a frame Destroying a frame destroys all its dependent dialogs. When the frame is iconified, its dependent dialogs disappear from the screen. When the frame is deiconified, its dependent dialogs return to the screen. A dialog can be modal. When a modal dialog is visible it blocks user input to all other windows in the program.

JDialog (cont) To create custom dialogs, use the JDialog class directly (as in the previous examples). Swing provides several standard dialogs JProgressBar, JFileChooser, JColorChooser,... The JOptionPane class can be used to create simple modal dialogs icons, title, text and buttons can be customized.

Example 3 Object[] options = {"Yes!", "No, I'll pass", "Well, if I must"}; int n = JOptionPane.showOptionDialog( frame, "Duke is a cartoon mascot. \n" + "Do you still want to cast your vote?", "A Follow-up Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);

JComponent JComponent is the base class for all Swing components except top-level containers. JLabel, JButton, JList, JPanel, JTable,... To use a component that inherits from JComponent, it must be placed in a containment hierarchy whose base is a top-level container.

JComponent The JComponent class provides the following (partial list): Pluggable Look & Feel Keystroke handling Tooltip support An infrastructure for painting Support for borders. All descendents of JComponent are also Containers A JButton can hold text, icons etc But not top-level containers

Borders Every JComponent can have one or more borders. The class JBorderFactory may be used to create standard borders pane.setborder(borderfactory. createlineborder(color.black)); Using a compound border, you can combine any two borders, which can themselves be compound borders BorderFactory.createCompoundBorder(border1, border2);

Simple Borders

Titled Borders

Compound Border

Intermediate Level Containers Also known as panels or panes Simplify the positioning of other components. JPanel JPanels are the default contentpane type Use a JPanel in setcontentpane() Play a visible and interactive role in a program s GUI JScrollPane JTabbedPane A panel s default layout manager is FlowLayout. Other layout managers can easily be set panel.setlayout(new BorderLayout());

Intermediate Level Containers (cont) By default, panels don t paint anything except for their background. By default, panels are opaque. An opaque panel can be set as a top-level container s content pane. transparent (non-opaque) panels draw no background.

Layout Management The process of determining the size and position of components. Layout management can be done using absolute positioning Size and position of every component within the container must be specified. Does not adjust well when the top-level container is resized. Does not adjust well to differences between users and systems, such as font size.

Layout Management (cont) Layout management is often performed using layout mangers Components can provide size and position hints to layout managers, but layout managers have the final say on the size and position of those components.

Layout Management (cont) Layout hints Minimum, preferred and maximum size X axis alignment, Y axis alignment Customizing layout hints Invoking setter methods: setminimumsize, setalignmentx,... Subclassing and overriding the getter methods: getminimumsize, getalignmentx,...

Layout Management (cont) The Java platform supplies five commonly used layout managers: BorderLayout BoxLayout FlowLayout GridLayout GridBagLayout

Layout Management (cont) When using the add method to put a component in a container, the container s layout manager must be taken into account. Relative position (BorderLayout) panel.add(component, BorderLayout.CENTER); Order of addition (BoxLayout, GridLayout,...) panel.add(component);

BorderLayout Has five areas available to hold components north, south, east, west and center All extra space is placed in the center area Only the center area is affected when the container is resized. Default layout manager of content panes.

BoxLayout Places components in a single row (left to right) or column (top to bottom). Respects component s maximum size and alignment hints.

FlowLayout Places components from left to right, starting new rows if necessary. Default LayoutManager of Jpanel Note: to avoid confusion with contentpane layout Jpanel mypanel; mypanel.setlayout(new BorderLayout()); setcontentpane(mypanel);

GridLayout Places components in a requested number of rows and columns. Components are placed left-to-right and top-to-bottom. Forces all components to be the same size as wide as the widest component's preferred width as high as the highest component s preferred height

Layout Management (cont) The following factors influence the amount of space between visible components in a container: Layout manager automatically, user specified, none Invisible components often used with BoxLayout Empty borders works best with components that have no default border such as panels and labels.

Events Handling Every time a user types a character or pushes a mouse button, an event occurs. Any object can be notified of an event by registering as an event listener on the appropriate event source. Multiple listeners can register to be notified of events of a particular type from a particular source.

Types of Event Listeners Act that results in event Listener type User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener

Implementing an Event Handler Implement a listener interface or extend a class that implements a listener interface. Register an instance of the event handler class as a listener upon one or more components. Implement the methods in the listener interface to handle the event.

Example 4 button.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { }}); numclicks++; label.settext(labelprefix + numclicks);

Getting started (with external libraries!) Swing is subdivided into packages: javax.swing, javax.accessibility, javax.swing.border At the start of your code - always import javax.swing.*; Most Swing programs also need import java.awt.*; import java.awt.event.*; UI Software Organisation IAT351 September 17, 2012