Java Swing Intro
Java Swing Intro
Agenda
• Introduction
• About JFC and Swing
• Pluggable Look and Feel
• Swing Components
• Borders
• Layout Management
• Events Handling
AWT to Swing
• AWT: Abstract Windowing Toolkit
• import java.awt.*
• Swing: new with Java2
• import javax.swing.*
• Extends AWT
• Tons o’ new improved components
• Standard dialog boxes, tooltips, …
• Look-and-feel, skins
• Event listeners
• API:
• http://java.sun.com/j2se/1.3/docs/api/index.html
Getting started with Swing
• Swing, like the rest of the Java API is subdivided
into packages:
– javax.swing, javax.accessibility,
javax.swing.border …
5
About JFC and Swing
• JFC – JavaTM Foundation Classes
• Encompass a group of features for constructing
graphical user interfaces (GUI).
• Implemented without any native code.
• “Swing” is the codename of the project that
developed the first JFC components .
• The name “Swing” is frequently used to refer to
new components and related API.
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 2DTM API
– Drag and Drop
• Between Java applications and native applications.
Pluggable Look and Feel
10
UI COMPONENTS
BUTTONS
MENUS
JMenuBar
OTHER COMPONENTS
JComboBox JColorChooser
ImageIcon
JInternalFrame
JDialog JFileChooser
14
OTHER COMPONENTS
JSplitPane JTabbedPane
OTHER COMPONENTS
JTable
JTextArea
JTextField
JToolBar
JToolTip
JTree
A simple Swing program - Containers
17
Containers
• Descendents of the java.awt.Container class
• Components that 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
– Depending on which layout manager is used
by the container
Remember this about Containers:
• The structure of containers is your design
decision and should always be thought
through in advance
– particularly for managing components
– nesting containers
19
Top Level Containers
• Every program that presents a Swing GUI
contains at least one top-level container.
• A Top level container provides the support that
Swing components need to perform their
painting and event-handling.
• Swing provides three top-level containers:
– JFrame (Main window)
– JDialog (Secondary window)
– JApplet (An applet display area within a
browser window)
Top Level Containers (cont)
• 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.
Screen
(x, y)
Frame
getHeight() screenHeight
getWidth()
screenWidth
Adding Components into a Frame
JFrame JFrame
JPanel containers
JPanel
JButton
JButton JLabel
JLabel
Using a GUI Component 2
1. Create it
2. Configure it order
important
3. Add children (if container)
4. Add to parent (if not JFrame)
5. Listen to it
Build from bottom up
• Create: Listener
• Frame
• Panel JLabel JButton
• Components
• Listeners
• Add: (bottom up) JPanel
• listeners into components
• components into panel
• panel into frame
JFrame
Application Code
import javax.swing.*;
class hello {
public static void main(String[] args){
JFrame f = new JFrame(“title”);
JPanel p = new JPanel();
JButton b = new JButton(“press me”);
createLineBorder(Color.black));
• Using a compound border, you can combine any two
borders, which can themselves be compound
borders
BorderFactory.createCompoundBorder(b
order1, border2);
Simple Borders
Titled Borders
Compound Border
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 Manager Heuristics
null FlowLayout GridLayout
none,
Left to right,
programmer
Top to bottom
sets x,y,w,h
s
Combinations
JButton JButton
JTextArea
Combinations
JButton JButton
JFrame
n JPanel: FlowLayout
JPanel: BorderLayout
c
JTextArea
Code: 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
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.
• A panel’s default layout manager is FlowLayout.
– Other layout managers can easily be set
panel.setLayout(new BorderLayout());
panel.add(component, BorderLayout.CENTER);
SOUTH
Container c = getContentPane();
c.setLayout(new BorderLayout() );
c.add (new JButton ("Next Slide"), BorderLayout.NORTH);
c.add (new JButton ("Previous Slide"), BorderLayout.SOUTH);
c.add (new JButton ("Back to Start"), BorderLayout.EAST);
c.add (new JButton ("Last Slide"), BorderLayout.WEST);
c.add (new JButton ("Exit"), BorderLayout.CENTER);
Layout Management — Using Panel
• Potential problem with BorderLayout:
– The button is stretched to fill the entire
southern region of the frame
– If you add another button to the
southern region, it would just displace
the first button
frame.setDefaultCloseOperation(JFrame.EXIT_ON_C
LOSE);
frame.pack(); pack() causes a window to be
sized to fit the preferred size and
frame.setVisible(true); layouts of its sub-components
}
Example 2
In this example a
import javax.swing.*; custom frame is
created
public class HelloWorldFrame extends JFrame {
public HelloWorldFrame() {
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) {
HelloWorldFrame frame = new
HelloWorldFrame();
}}
Menus
• You can add a JMenuBar object to JFrame or
JApplet.
• You can add JMenu objects to a JMenuBar.
• You can add other JMenus, JMenuItems,
JCheckBoxMenuItems, JRadioButtonMenuItems,
etc. to a Jmenu.
Menus
Menu Bar Menu JMenuItem( String )
– JMenuBar() – JMenu( String ) JMenuItem( String,int )
– add( JMenu ) – add( JMenuItem )
JMenuBar mb = new JMenuBar(); //create a menu bar
JMenu fileMenu = new JMenu (“File”); //create a menu
mb.add( fileMenu ); //add menu to menu bar
setMenuBar( mb ); // add a menu bar to frame
fileMenu.setMnemonic( KeyEvent.VK_F ); // add a hotkey to menu
• Methods
– Can declare label text in constructor
– myLabel.setToolTipText( "Text" )
• Displays "Text" in a tool tip when mouse over
label
– myLabel.setText( "Text" )
– myLabel.getText()
JLabel
• Icon
– Object that implements interface Icon
24 Icon bug = new ImageIcon( "bug1.gif" );