Using JComboBoxes in Applets

In the code below note the case statement with strings instead of integers as the control variables. This is possible in Oxygene for Java but not in Pascal. The class implements the itemStateChanged method of the ItemListener interface to react to selection of an item in the JComboBox. We needed the instruction cbo.setEditable(true) to allow the end user to add other colours.

This screenshot shows the applet in action. Yellow has now been added to the drop-down list of items to select.

JComboBox applet in action

JComboBox applet in action

The code of the JComboBox applet

namespace combo_demo;

interface

uses
  java.util, java.applet.*, java.awt.*, javax.swing;

type
  ComboDemo = public class(Applet, ItemListener)
  private
    lblInstruction, lblSelected : JLabel;
    cbo : JComboBox;
    colour : String;
  public
    method init; override;
    method itemStateChanged(e : ItemEvent);
  end;

implementation

method ComboDemo.init;
begin
  Background := Color.gray;
  lblInstruction := new JLabel('<html>Please enter yellow,<p>magenta, white or black');
  lblInstruction.setForeground (Color.blue.brighter);
  add(lblInstruction);
  lblSelected := new JLabel('                    ');
  cbo := new JComboBox;
  cbo.addItemListener(self);
  cbo.addItem('red');
  cbo.addItem('green');
  cbo.addItem('blue');  
  cbo.setSelectedItem('red');
  cbo.setEditable(true);
  add(cbo);
  add(lblSelected);
end;

method ComboDemo.itemStateChanged(e : ItemEvent);
begin
  if e.getSource = cbo then
    begin 
      colour := cbo.getSelectedItem.toString;
      lblSelected.setText(colour);
      case colour.toLowerCase of
        'red' : lblSelected.setForeground(Color.red);
        'yellow' : lblSelected.setForeground(Color.yellow);
        'green' : lblSelected.setForeground(Color.green);
        'blue' : lblSelected.setForeground(Color.blue);
        'magenta' : lblSelected.setForeground(Color.magenta);
        'white' : lblSelected.setForeground(Color.white);
        'black' : lblSelected.setForeground(Color.black);
     else
       lblSelected.setText('');
     end;        
   end;
end;

end.

Changing the layout and instruction label

We positioned the widgets differently using a BoxLayout and added individual colouring to the instruction label text to obtain this screenshot.

JComboBox applet (300 x 80) with BoxLayout

JComboBox applet (300 x 80) with BoxLayout

These two lines of code replacing the second instruction of the init method created the changes.

  setLayout(new BoxLayout(self, BoxLayout.PAGE_AXIS));
  lblInstruction := new JLabel('<html>Please enter <font color=yellow>yellow</font>,<p>' +
                               '<font color=#ff00ff>magenta</font>, <font color=white>white</font>' +
                               ' or <font  color=black>black</font></html>', SwingConstants.LEFT);
Programming - a skill for life!

How to use widgets such as JButtons, JTextAreas, JComboBoxes and dialogues in Oxygene for Java applets