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 Use Cases User actor Design GUI Java Foundation Classes Amadeo Ascó , Adam Moore Add record View record Edit record Save record Delete record Database actor 2 Overview • Hierarchy • Overview Swing Components • Swing Components Amadeo Ascó , Adam Moore 3 Hierarchy java.lang Object java.awt Component Container Abstract Window Toolkit (AWT) javax.swing JComponent JLabel JMenuBar JButton JTextField JPanel JComboBox JTable Amadeo Ascó , Adam Moore JCheckBox JTextArea 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 • JCheckBox • JComboBox • JRadioButton • JPanel • JScrollBar • JSlider • JSplitPane • JPopupMenu Divider between panels can be drag left and right Amadeo Ascó , Adam Moore 6 Swing Components • JTree • JMenuBar • • • • • JMenu JMenuItem JTextField JTabbedPane JTextArea SubSuperClasses bin doc readme.txt javadoc src • JLabel Fix text on the display Application title Tab 1 Tab 2 Tab 3 Panel • JDialog • JTabel Amadeo Ascó , Adam Moore Data was saved. 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 Hello World height public class HelloWorldGUI extends JFrame { ... 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.", "Message"); – Custom title, warning icon Data was saved. 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 menuBar = new JMenuBar(); JMenuBar //Build the first menu JMenu menu = new JMenu("1st Menu"); JMenu menuBar.add(menu); // add menu to menu bar // Add menu items JMenuItem menuItem = new JMenuItem("1st Option"); menuItem.addItemListener(this); // register the listener menu.add(menuItem); // add item to a container add(JMenu) add(JMenuItem) add(JMenu) JMenuItem addItemListener(ItemList ener) JFrame 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