G54PRG Programming
Lecture 23
GUI Programming II: Components
Amadeo Ascó
Adam Moore
1
Previously
• Hello World as GUI
• Layout Design
• Criteria In GUI
• Human Interface Concepts
• Requirements
Add record
View record
• Use Cases User
Edit record
Save record Database
•
actor
Design GUI actor Delete record
• Java Foundation Classes
Amadeo Ascó , Adam Moore 2
Overview
• Hierarchy
• Overview Swing Components
• Swing Components
Amadeo Ascó , Adam Moore 3
Hierarchy
Object java.lang
java.awt
Component
Abstract Window Toolkit
Container (AWT)
JComponent javax.swing
JLabel JMenuBar JTextField JComboBox JCheckBox
JButton JPanel JTable JTextArea
Amadeo Ascó , Adam Moore 4
Swing Components
• Position Layout Managers
• Look how the control is displayed on the
screen
• Feel - base functionality
– Do you click on them. e.g. button (JButton)
– Do you type on them, e.g. text field (JTextField) ...
• Functionality Events
– What happen when the corresponding action has
been executed by the user, e.g. clicking on the "Save"
button save the data
Amadeo Ascó , Adam Moore 5
Overview Swing Components
• JButton • JPanel
• JScrollBar
• JCheckBox
• JSlider
• JComboBox
• JSplitPane
• JRadioButton
• JPopupMenu Divider between panels can
be drag left and right
Amadeo Ascó , Adam Moore 6
Swing Components
• JMenuBar • JTree SubSuperClasses
bin
doc
readme.txt
javadoc
src
• JMenu • JLabel
• JMenuItem Fix text on the display
• JTextField • JDialog Data was saved.
Application title
• JTabbedPane
Tab 1 Tab 2
Panel
Tab 3
• JTabel
• JTextArea
Amadeo Ascó , Adam Moore 7
Swing Components
import javax.swing.JFrame;
import javax.swing.JLabel;
public class HelloWorldGUI extends JFrame {
public static void main(String[] args) {
new HelloWorldGUI();
} // main()
HelloWorldGUI() {
super("Hello World"); // the title
JLabel jlbHelloWorld = new JLabel(" Hello World!");
add(jlbHelloWorld); // add label to the GUI
setSize(150, 80); // size of the window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // show the GUI
} // Constructor ()
} // end class HelloWorldGUI
Amadeo Ascó , Adam Moore 8
Swing Components
• Extend JFrame to get the general look of a GUI
• JLabel corresponds to fix text to add to the GUI
public class HelloWorldGUI extends JFrame {
... Hello World
height
HelloWorldGUI() {
super("Hello World"); // the title
JLabel jlbHelloWorld = new JLabel(" Hello World!"); width
add(jlbHelloWorld); // add label to the GUI
setSize(150, 80); // size of the window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // show the GUI
} // Constructor ()
} // end class HelloWorldGUI Amadeo Ascó , Adam Moore 9
Swing Components
• Creating and Showing Simple Dialogs
– It is a accomplished by using the class JDialog
– Different ones depending of the message to display
– Default title and icon
JOptionPane.showMessageDialog(frame, Data was saved.
"Data was saved.", "Message");
– Custom title, warning icon Warning!
Data was saved.
JOptionPane.showMessageDialog(frame,
“Data was saved.", "Warning!", JOptionPane.WARNING_MESSAGE);
– Custom title, error icon Error!
Data wasn’t saved.
JOptionPane.showMessageDialog(frame,
"Data wasn’t saved.", “Error!", JOptionPane.ERROR_MESSAGE);
Amadeo Ascó , Adam Moore 10
Swing Components
• Yes/No dialog with default icon, custom title
int iAnswer = JOptionPane.showConfirmDialog(frame, "Would you like to
save the data?", "Question!", JOptionPane.YES_NO_OPTION);
Valid return values
• JOptionPane.YES_OPTION: user has selected 'YES'
• JOptionPane.NO_OPTION: user has selected 'NO'
• JOptionPane.CLOSED_OPTION: user closed the dialog window
explicitly, rather than by choosing a button inside the option pane
Question!
Would you like to save the data?
Amadeo Ascó , Adam Moore 11
Swing Components
• JMenuBar and JMenuItem
//Create the menu bar JMenuBar
JMenuBar menuBar = new JMenuBar(); add(JMenu)
//Build the first menu JMenu
JMenu menu = new JMenu("1st Menu"); add(JMenuItem)
menuBar.add(menu); // add menu to menu bar add(JMenu)
JMenuItem
// Add menu items
addItemListener(ItemList
JMenuItem menuItem = new JMenuItem("1st Option");
ener)
menuItem.addItemListener(this); // register the listener JFrame
menu.add(menuItem); // add item to a container setJMenuBar(JMenuBar)
menuItem = new JMenuItem("2nd Option");
menuItem.addItemListener(this); // register the listener
menu.add(menuItem); // add item to a container
...
jFrame.setJMenuBar(menuBar); // set menu bar
Amadeo Ascó , Adam Moore 12
Swing Components
• JTextField JTextField jText = new JTextField(15);
...
add(jText); // add to a container
...
jText.getText(); // get text on the text fields
• JButton JButton jButton = new JButton("OK");
jButton.setActionCommand ("OK"); // assign an action identifier
jButton.addActionListerner(this); // register the listener
add(jButton); // add to a container
...
Amadeo Ascó , Adam Moore 13
Swing Components
• JTable
// Header
String[] astrColumnNames =
{"First name", "Last name", "Num", "Road", "Alive"};
Object[][] aoRows = {
{"Debby", "Smith", new Integer(5), "Parliament", new Boolean(false)},
{"John", "Rainbow", new Integer(3), "Great Av.", new Boolean(true)},
{"Jason", "Wood", new Integer(2), "Lower line", new Boolean(false)},
{"Jane", "White", new Integer(13), "High Street", new Boolean(true)}
};
// Build table using these roes and column names
JTable table = new JTable(aoRows, astrColumnNames);
Amadeo Ascó , Adam Moore 14
References
A Brief Introduction to the Swing Package
http://download.oracle.com/javase/tutorial/ui/overview/index.html
Using Swing Components
http://download.oracle.com/javase/tutorial/uiswing/components/
index.html
Java API
http://download.oracle.com/javase/6/docs/api/
Course Resources Website
http://workspace.nottingham.ac.uk/display/G54ICP/Resources
Java look and feel Graphics Repository
http://java.sun.com/developer/techDocs/hi/repository/
Amadeo Ascó , Adam Moore 15