Lecture : Graphical User Interface Programming 2
In the previous lecture:
In this lecture:
Consult your Java manual for details of the UI elements described below. We'll discuss them briefly in class.
An iconised, rollover button
... |
A check box that changes the font in a text field
... // define a text field and set its font |
Radio buttons in a group
// Make two radio buttons with initial values of true and false button1 = new JRadioButton("Button 1", true); button2 = new JRadioButton("Button 2", false); // Add these two radio buttons to the same group so that when // one is switched on, the other automatically switches off radioGroup = new ButtonGroup(); radioGroup.add(button1); radioGroup.add(button2); // In an ItemListener event handler examine // e.getSource() == button1 or button2 // to determine which button was pushed. |
A JComboBox that changes the icon in a JLabel
import [...] public class ComboBoxTest extends JFrame public ComboBoxTest() { super ("Combo Boxed"); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.white); images = new JComboBox (names); // add the text entries for the pull down menu images.setMaximumRowCount(2); // maximum length of the menu bar that drops down before a scroll bar is added images.addItemListener ( new ItemListener() { public void itemStateChanged (ItemEvent e) { label.setIcon(icons[ images.getSelectedIndex() ]); } } ); label = new JLabel(icons[0]); c.add(images); c.add(label); } public static void main (String args[]) { ComboBoxTest app = new ComboBoxTest(); app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); app.setSize (200,75); app.setVisible(true); } } |
Try it yourself to see how it works! |
Homework: | Investigate JList (for single and multi-selection lists). |
Handling Mouse Events
import [...] public class MouseEventsTest extends JFrame implements MouseListener, MouseMotionListener addMouseListener(this); addMouseMotionListener(this); } // MouseListener events... public void mouseClicked (MouseEvent e) { statusBar.setText("Click at [" + e.getX() + ", " + e.getY() + "]"); } public void mousePressed (MouseEvent e) { statusBar.setText("Press at [" + e.getX() + ", " + e.getY() + "]"); } public void mouseReleased (MouseEvent e) { statusBar.setText("Release at [" + e.getX() + ", " + e.getY() + "]"); } public void mouseEntered (MouseEvent e) { statusBar.setText("Mouse inside window"); } public void mouseExited (MouseEvent e) { statusBar.setText("Mouse outside window"); } // MouseMotionListener events... public void mouseDragged (MouseEvent e) { statusBar.setText("Drag at [" + e.getX() + ", " + e.getY() + "]"); } public void mouseMoved (MouseEvent e) { statusBar.setText("Moved at [" + e.getX() + ", " + e.getY() + "]"); } public static void main (String args[]) { MouseEventsTest app = new MouseEventsTest(); app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); app.setSize (250,100); app.setVisible(true); } } |
The text is updated as the different mouse handlers are invoked. This one needs to be executed to be properly understood. Once you've understood what is happening, read up on the KeyListener and see if you can use it. |
Homework: | Investigate the adapter classes ComponentAdapter, ContainerAdapter, FocusAdapter, KeyAdapter, MouseAdapter, MouseMotionAdapter and WindowAdapter. What are they for? Why are they useful? Write some simple code to use the MouseAdapter to see if you understood how it works. Note that all of our examples so far have used a WindowAdapter. Why? |