an introduction to java swing - comscigate.com filean introduction to java swing • what is java...

34
TAKE IT TO THE NTH An Introduction to Java Swing

Upload: votu

Post on 15-Jul-2019

324 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

T A K E I T T O T H E N T H

An Introduction to Java Swing

Page 2: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Asif Habibullah

Member of Technical Staff, Software

Market Development Engineering

In Introduction to Java Swing

T A K E I T T O T H E N T H

Page 3: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

An Introduction to Java Swing• What is Java Swing?

• Swing GUI Fundamentals

• Swing Components

• Event Handling

• Model-View-Controller (MVC) Design Pattern

• Rich clients for Web Services

• Deploy via Java Web Start

Page 4: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

What is Java Swing?

• Graphical User Interface (GUI) component toolkit.

• Complete set of user-interface elements written entirely in Java.

• Integrated into the Java 2 platform (1.2 or higher).

• No need to download and install on Java 2 platform.

• If running Java 1.1, you can download and install separately.

• Part of Java Foundation Classes (JFC)

Page 5: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Java Foundation Classes (JFC)� The Java Foundation Classes (JFC) extend the original Abstract

Window Toolkit (AWT) by adding a comprehensive set of graphical user interface class libraries

• What makes up the JFC (features)?

• Swing

• Accessibility API

• Java 2D API

• Drag and Drop API

• AWT

• Internationalization

Page 6: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

What about AWT?

Swing AWT• Lightweight components

• Complex components

• Pluggable Look and Feel

• Pure Java – Platformindependent

• Heavyweight components

• Primitive components

• Single Look and Feel

• Not Pure Java – Platform dependent

Page 7: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Lightweight vs. Heavyweight

Lightweight Heavyweight• Not dependent on native peers

• Rely on primitive Java drawingtechniques in rendering.

• Components behavior is thesame across platforms.

• Look and Feel is independentof platform

• Dependent on native peers

• Rely on native windowingsystem in rendering.

• Component behavior maydiffer across platforms.

• Look and feel is dependenton platform

Page 8: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Swing GUI Fundamentals

• Swing components • Containers• Non-containers

• Layout Mangers

• Event Handling �

Page 9: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Swing Containers

� Used to hold other Swing containers and components.

� Two main types of containers:

– 1) Top level containers: � Root of the containment heirarchy� e.g. JFrame, JDialog, JApplet

– 2) Intermediate containers: � Middle nodes of containment heirarchy� e.g. JPanel, JScrollPane, JSplitPane, ...

� GUIs are usually made up of nested containers.

Page 10: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Swing Non-containers

� All of the other GUI elements that are not used to hold orpositions other GUI elements.

� 2 categories of non-containers:

� JButton� JComboBox� JTextField� JTable...

� JLabel� JToolTip� JProgressBar...

User input Data display/organization

Page 11: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Example: Swing Components

JButton

JComboBox

JTable

JFrame

JScrollPane

JTabbedPane

JMenuBar

JLabel

Page 12: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Layout Managers� Used by containers to position and organize their

components.

� Common Layout Managers:

– FlowLayout

– BorderLayout

– BoxLayout

– CardLayout

– GridLayout

– GridBagLayout

� Set container's layout by calling : Container.setLayout(LayoutManager manager)

Page 13: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

FlowLayout

� FlowLayout lays out components left to right creating new rows as necessary.

� Default for JPanel.� Container.add(Component comp)

Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Page 14: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

BorderLayout

� BorderLayout can hold components in the 5 areas: north, south, east, west, center.

� Default for JFrame.� Container.add(Component comp, int index)

// e.g. - index = BorderLayout.NORTH

Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Page 15: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

BoxLayout

� BoxLayout simply positions components in a rows or columns.

� Example:

Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Page 16: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

CardLayout

� A card layout is used when one wants to display different components at different times. A controller such as a JComboBox is normally used to flip through the different cards.

� Example:

Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Page 17: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

GridLayout

� GridLayout will make all components the same size by placing them in a grid of specified row and column count.

� Example:

Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Page 18: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

GridBagLayout

� Places components in a grid of rows and columns allowing specificed components to span mulitple rows and/or columns.

� Most flexible and robust of all layout managers.

� Example:

Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Page 19: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Example: Swing LayoutJP

anel

- Nor

th

JFram

e - B

orde

rLay

out

JPan

el - C

enter

JP

an

el

-S

ou

th

JP

an

el

-

Fl

ow

La

yo

ut

JP

an

el

-F

lo

wL

ay

ou

t

Page 20: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Event Handling� The way a program handles user interactions with the

GUI

� Type of events:

– Mouse clicks

– Keyboard events

� Whenever an event occurs, all listeners that subscribe to that event are notified.

� For an object to be able to subscribe to an event, it just needs to implement the appropriate listener interface.

Event Source EventObject Event Listener

Page 21: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Event Handling (cont.)

� 2 key pieces of code:

1) Create the Listener class that implements the correct methods

2) Register the Listener with the component

� Easiest implementation: Use an Anonymous class

– Example:� JButton jb = new JButton();jb.addActionListener(new ActionListener(){

public void actionPerformed(Event e){System.err.println("Button clicked");

}});

Page 22: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Multiple Listeners

� Possible to register multiple listeners to a single component.

JButton ActionListener

MouseListener

FocusListener

Detect button click

Detect mouse over

Detect focus gain

Page 23: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

The Event Thread

� When an event is dispatched, the code that is executed runs in the Event Thread.

� Code that runs in the event thread should execute very quickly.

� Time-consuming tasks:

– Loading of a file

– Running a benchmark

� These tasks should be run in their own thread.

Page 24: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

SwingWorker Class� SwingWorker is a class that should be used to perform

time consuming tasks not to be run in the event thread.

Button Down

Load

Button Up

Button Down

Load

Button Up

Without SwingWorker With SwingWorker

Page 25: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

SwingWorker - How to use it� In the Event Handling code:

– Create a class that extends SwingWorker.

– Override the public Object SwingWorker.construct() method with the time-consuming code (Loading, etc.)

– Override the public void SwingWorker.finished() method to execute code that MUST be executed after the completion of the SwingWorker Thread.

– Call SwingWorker.start()

� SwingWorker.finished() is run in the Event Thread after the SwingWorker thread is complete.

� Visit for details: http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

Page 26: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Model-View-Controller(MVC) Design Pattern

� Model – An internal representation of the data displayed by the view.

� View – The interface that the user manipulates.

� Controller – Used to change state when the view is changed by the user.

View

Model

ControllerUser Interaction

Invoke State ChangeInvoke View Change

Page 27: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

MVC - A Simple Example� SpreadSheet Application

– Model = TableModel

– View = JTable

– Controller = CellEditorListener

JTable

TableModel

CellEditorListenerUser Interaction

Invoke State ChangeInvoke View Change

Page 28: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Rich Clients for Web Services

� Talk given last year at Java One (2001) by Howard Rosen, Hans Muller, Scott Violet of the Java Client Group

� Details can be found at: http://java.sun.com/products/jfc/tsc/articles/javaOne2001/2734/index.html

� Summary: Rich Clients + Web Services = Best Web Experience

� Web Services

– Not only for browser based applications.

– Create rich clients using Java Technologies.

– Deploy Java applications via Java Web Start

Page 29: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Java [tm] Technology Web Services Model

Page 30: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Why Build Rich Clients?

� Dynamic, Responsive, Easy to use

� Competitive edge, not just HTML

� Real-time visualization

� Decouple server performance from GUI

� Support offline usage

Page 31: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Client - Server Communication

� Speak HTTP just like your browser-based applications.

� Server-side code does not need to change.

� Low bandwidth connection to the server

� 1 second round trips typical

– If > 1 second; use a worker thread to communicate.� Just like SwingWorker

– Always keep GUI reponsive

Page 32: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Deploying via Java Web Start� Single click deployment:

– User clicks on a link in a browser

– Application starts

� Eliminates the install step

� Based on JSR-56: Java Network Launching Protocol and API

� Downloaded application runs outside of the browser, unlike an applet.

� Guaranteed to run the most current version of the application.

Page 33: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

Summary

� Java Swing provides developers to create complex 100% Java based GUIs.

� A whole lot more out there that we did not talk about:

– Pluggable Look and Feel

– Drag and Drop

– Internationalization ...

� The Java Tutorial

– http://java.sun.com/docs/books/tutorial/

Page 34: An Introduction to Java Swing - comscigate.com fileAn Introduction to Java Swing • What is Java Swing? • Swing GUI Fundamentals • Swing Components • Event Handling • Model-View-Controller

T A K E I T T O T H E N T H

Asif Habibullah

[email protected]