Creating a GUI
with Swing
Introduction
• Very useful link:
http://java.sun.com/docs/books/tutorial/uiswing/index.html
• Swing
– is a part of JFC (Java Foundation Classes)
JFC is a group of features for building graphical
user interfaces (GUIs).
– is a set of widgets for Java
widget is an element of GUI (aka: component)
– is a part of the Java's standard library
– intended to replace corresponding components of
AWT (Abstract Window Toolkit).
Swing Components
• Containers
to contain and to manage other components
Top-Level Containers
(JFrame, JWindow, JApplet and JDialog)
General-Purpose Containers
(JPanel, JScrollPane, JTabbedPane and so on)
Special-Purpose Containers
• Basic Components
for interaction with a user
Uneditable Information Displays
(JLabel, JToolTip and so on)
Basic Controls
(JButton, JCheckBox, JTextField and so on)
…
Swing Components (examples)
Containers:
Basic components:
JFrame
• Made from several layers (Special-Purpose Containers)
• Components are added to the Content Pane layer.
• The order of the components is determined according to
the layout manager
Layouts
to set a layout of a container:
void setLayout(LayoutManager lm)
6
Swing Example
import javax.swing.*;
import java.awt.BorderLayout;
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame("My First Frame");
JPanel p = new JPanel(new BorderLayout());
f.add(p);
JLabel l = new JLabel("My First Label");
p.add(l, BorderLayout.NORTH);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Events and Listeners
Event
• just object
• stores information about a type of event, its source etc.
• is created on certain actions in an application
Listener
• event handler
• is an object that implements a listener interface
• when an event occurs, all registered listeners are notified
the appropriate listener method is invoked
an object describing the event is passed as a
parameter
• to react to an event (on a certain component) a listener
object should be registered with that component
Listeners Example
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Events {
public static void main(String[] args){
JFrame f = new JFrame();
f.getContentPane().setLayout(new FlowLayout());
JButton b = new JButton("Click me!");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Thank you");
}
});
f.getContentPane().add(b);
f.pack();
f.setVisible(true);
}
}
MVC
• Model
represents the data for the application
• View
the visual representation of that data
• Controller
takes user input on the view and translates that to changes in
the model
JTable
• Separation of the table view from the table model
• JTable – view
• TableModel – model
• DefaultTableModel – a default implementation of
TableModel
• DefaultTableModel can be customized by
subclassing
• TableModel can be implemented from scratch by
subclassing
• TableModelListener – a listener interface
• Notifies model of changes in the view