Download - GUI Programming II: Components
1
G54PRG ProgrammingLecture
1
Amadeo Ascó Adam Moore
23
GUI Programming II: Components
2Amadeo Ascó , Adam Moore
Previously
• Hello World as GUI• Layout Design• Criteria In GUI• Human Interface Concepts• Requirements• Use Cases• Design GUI• Java Foundation Classes
Add record
View record
Edit record
Save record
Delete recordactorUser Database
actor
3Amadeo Ascó , Adam Moore
Overview
• Hierarchy• Overview Swing Components• Swing Components
4Amadeo Ascó , Adam Moore
HierarchyObject
Component
Container
JLabel
JButton
JMenuBar
JPanel JTable
JCheckBoxJComboBoxJTextField
JTextArea
JComponent
java.awt
javax.swing
Abstract Window Toolkit (AWT)
java.lang
5Amadeo Ascó , Adam Moore
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
6Amadeo Ascó , Adam Moore
• JPanel
• JScrollBar
• JSlider
• JSplitPane
• JButton
• JCheckBox
• JComboBox
• JRadioButton
• JPopupMenu
Overview Swing Components
Divider between panels can be drag left and right
Divider between panels can be drag left and right
7Amadeo Ascó , Adam Moore
• JMenuBar
• JMenu • JMenuItem• JTextField• JTabbedPane• JTextArea
• JTree
• JLabel Fix text on the display
• JDialog• JTabel
Swing ComponentsSubSuperClasses
src
docreadme.txtjavadoc
bin
Tab 1 Tab 2 Tab 3
Application title
Panel
Data was saved.
8Amadeo Ascó , Adam Moore
Swing Componentsimport 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
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
9Amadeo Ascó , Adam Moore
public class HelloWorldGUI extends JFrame { ...
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
public class HelloWorldGUI extends JFrame { ...
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
Swing Components
• Extend JFrame to get the general look of a GUI• JLabel corresponds to fix text to add to the GUI
Hello World
width
he
igh
t
10Amadeo Ascó , Adam Moore
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.", "Message");
– Custom title, warning iconJOptionPane.showMessageDialog(frame,“Data was saved.", "Warning!", JOptionPane.WARNING_MESSAGE);
– Custom title, error iconJOptionPane.showMessageDialog(frame,"Data wasn’t saved.", “Error!", JOptionPane.ERROR_MESSAGE);
Data was saved.
Data was saved.
Warning!
Data wasn’t saved.
Error!
11Amadeo Ascó , Adam Moore
Swing Components
• Yes/No dialog with default icon, custom titleint 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
Would you like to save the data?
Question!
12Amadeo Ascó , Adam Moore
Swing Components• JMenuBar and JMenuItem//Create the menu barJMenuBar menuBar = new JMenuBar();
//Build the first menuJMenu menu = new JMenu("1st Menu");
menuBar.add(menu); // add menu to menu bar
// Add menu itemsJMenuItem menuItem = new JMenuItem("1st Option");
menuItem.addItemListener(this); // register the listenermenu.add(menuItem); // add item to a container
menuItem = new JMenuItem("2nd Option");menuItem.addItemListener(this); // register the listenermenu.add(menuItem); // add item to a container...jFrame.setJMenuBar(menuBar); // set menu bar
//Create the menu barJMenuBar menuBar = new JMenuBar();
//Build the first menuJMenu menu = new JMenu("1st Menu");
menuBar.add(menu); // add menu to menu bar
// Add menu itemsJMenuItem menuItem = new JMenuItem("1st Option");
menuItem.addItemListener(this); // register the listenermenu.add(menuItem); // add item to a container
menuItem = new JMenuItem("2nd Option");menuItem.addItemListener(this); // register the listenermenu.add(menuItem); // add item to a container...jFrame.setJMenuBar(menuBar); // set menu bar
JMenuBaradd(JMenu)
JMenuadd(JMenuItem)add(JMenu)
JMenuItemaddItemListener(ItemListener)
JFramesetJMenuBar(JMenuBar)
13Amadeo Ascó , Adam Moore
Swing Components
• JTextField
• JButton
JTextField jText = new JTextField(15);...
add(jText); // add to a container...jText.getText(); // get text on the text fields
JTextField jText = new JTextField(15);...
add(jText); // add to a container...jText.getText(); // get text on the text fields
JButton jButton = new JButton("OK");
jButton.setActionCommand ("OK"); // assign an action identifierjButton.addActionListerner(this); // register the listeneradd(jButton); // add to a container...
JButton jButton = new JButton("OK");
jButton.setActionCommand ("OK"); // assign an action identifierjButton.addActionListerner(this); // register the listeneradd(jButton); // add to a container...
14Amadeo Ascó , Adam Moore
Swing Components
• JTable// HeaderString[] 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 namesJTable table = new JTable(aoRows, astrColumnNames);
// HeaderString[] 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 namesJTable table = new JTable(aoRows, astrColumnNames);
15Amadeo Ascó , Adam Moore
ReferencesA Brief Introduction to the Swing Package
http://download.oracle.com/javase/tutorial/ui/overview/index.html
Using Swing Componentshttp://download.oracle.com/javase/tutorial/uiswing/components/
index.html
Java APIhttp://download.oracle.com/javase/6/docs/api/
Course Resources Websitehttp://workspace.nottingham.ac.uk/display/G54ICP/Resources
Java look and feel Graphics Repositoryhttp://java.sun.com/developer/techDocs/hi/repository/