CSE5910 : Multimedia Programming in Java

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

...
Icon tree = new ImageIcon("Tree.GIF"); Icon tent = new ImageIcon("Tent.GIF"); button = new JButton ("Icon Button", tree); button.setRolloverIcon(tent); ...

A check box that changes the font in a text field

...
// define a text field and set its font
text = new JTextField("Default text", 10); text.setFont ( new Font ("Times.Roman, Font.PLAIN, 12) ); // define a check box checkBold = new JCheckBox("Bold"); [... insert code to add these to a window ... ] // set up an event handler CheckBoxHandler handler = new CheckBoxHandler(); checkBold.addItemListener(handler); [... insert code to open the window etc. ...] // the event handler class private class CheckBoxHandler implements ItemListener { private int valueText = Font.PLAIN; public void itemStateChanged (ItemEvent e) { if (e.getSource() == checkBold) { if (e.getStateChange() == ItemEvent.SELECTED) { valueText = Font.BOLD; } else { valueText = Font.PLAIN; } text.setFont( new Font (Times.Roman, valueText, 12) ); text.repaint(); } } } ...

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
{
private JComboBox images;
private JLabel label;

private String names[] = { "Tent.gif", "Tree.gif" };
private Icon icons[] = { new ImageIcon(names[0]), new ImageIcon(names[1]) };
  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
{
private JLabel statusBar;

public MouseEventsTest()
{
super ("Mouse Events");
statusBar = new JLabel();
getContentPane().add(statusBar, BorderLayout.SOUTH);
      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?



Lecture summary:


CSE5910 Courseware | CSE5910 Lecture Notes