A Quick Java Swing Tutorial
A Quick Java Swing Tutorial
Introduction
Swing A set of GUI classes
Part of the Java's standard library Much better than the previous library: AWT Abstract Window Toolkit
Highlights
A rich set of widgets Widget: Any GUI element (also called: components) Contents and shape are separated (MVC support) Fine-grained control over the behavior and look and feel Platform independent Isolates the programmer from the operating system's GUI
2
Swing Components
Containers Contain and manage other components. Top Level/Internal Examples: JFrame (Top Level), JScrollPane, JPanel. Basic controls Atomic components Used for showing ouput and/or getting some input Inherits JComponent Examples: JButton, JLabel, JTextArea, JTable, Jlist Usually every Swing class extends the corresponding AWT class For backward-compatibility reasons
3
javax.swing.JFileChooser:
Allows the the user to choose a file Supportsopenandsave:showOpenDialog(),showSaveDialog()
JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION) System.out.println("File: " + fc.getSelectedFile());
6
More on JFrame
Made of several layers Widgets are added to the Content Pane layer. Use getContentPane() to obtain it Other layers are used for customizing the window's appearence
Internal Containers
Not Top level containers Can contain other non-top level components Examples:
JScrollPane: Provides a scrollable view of its components
JSplitPane: Separates two components
Containers - Layout
Each container has a layout manager Determines the size, location of contained widgets.
classes:
10
Containers - Layout
11
Swing Components
12
Swing Components
13
layout
Input
So we now know how to present widgets on the screen A program also needs to react to the user's actions Examples:
When the user presses a button we want to save a file Whentheuserclosestheprogramwewanttoaskareyou sure? ...
15
Events, Listeners
Swing defines all sorts of Listener interfaces E.g.: ActionListener, MouseMotionListener, WindowListener, ...
public interface ActionListener extends EventListener { public void actionPerformed(ActionEvent e); } public interface MouseMotionListener extends EventListener { public void mouseDragged(MouseEvent e); public void mouseMoved(MouseEvent e); }
There are default (empty) implementations for many of the listeners E.g.: MouseMotionAdapter, WindowAdapter
16
17
18
frame.pack(); frame.setVisible(true);
} public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Thank you"); } public static void main(String[] args) { new Events(); } }
19
Inner Classes
Nested within another classes Instance specific:
Has access to methods & fields of the object that created it => An inner class has TWO this variables
Can be static
Can access only static members and methods only A static method cannot create a non-static inner class
20
Local Classes
Same as inner classes but defined inside a method Has access to local variables of the enclosing method Only if the variable is defined as final Can be anonymous Doesnthaveaname.
21
public class Events { public Events() { JFrame frame = new JFrame("Events"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new FlowLayout()); JButton b = new JButton("Click me!"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Thank you"); } }); frame.getContentPane().add(b);
frame.pack(); frame.setVisible(true);
} public static void main(String[] args) }
22
{ new Events(); }
// Output: 5
// Output: 1024
25
Swing's JTable
Each JTable has a TableModel object that holds the data shown by the table.
DefaultTableModel is a default implementation of TableModel By default it makes all the cells editable
We can customize its behavior by subclassing Or we can implement TableModel from scratch
TableModelListener - a listener interface
Operations:
Add contact
Contact name is immutable
Add attributes
Attributeisidentifiedbyitsname,theattributenameisimmutable,itsvalue can be changed
Classes:
Contact: represents a contact and maintains list of attributes Contact.Attribute: Inner class that represents contact attribute. ContactTableModel: model class for the table data
Doesntallowtomodifyattributenames
27
28