an introduction to java swing - com sci gate · an introduction to java swing. asif habibullah...
TRANSCRIPT
T A K E I T T O T H E N T H
An Introduction to Java Swing
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
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
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)
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
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
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
Swing GUI Fundamentals
• Swing components • Containers• Non-containers
• Layout Mangers
• Event Handling �
�
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.
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
Example: Swing Components
JButton
JComboBox
JTable
JFrame
JScrollPane
JTabbedPane
JMenuBar
JLabel
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)
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
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
BoxLayout
� BoxLayout simply positions components in a rows or columns.
� Example:
Source: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
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
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
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
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
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
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");
}});
Multiple Listeners
� Possible to register multiple listeners to a single component.
JButton ActionListener
MouseListener
FocusListener
Detect button click
Detect mouse over
Detect focus gain
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.
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
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
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
MVC - A Simple Example� SpreadSheet Application
– Model = TableModel
– View = JTable
– Controller = CellEditorListener
JTable
TableModel
CellEditorListenerUser Interaction
Invoke State ChangeInvoke View Change
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
Java [tm] Technology Web Services Model
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
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
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.
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/