5.2.6 Other GUI Components


In this subsection we list the constructors and main methods for GUI components that we have not treated yet. Here, we shall exemplify the components to their bare minimum, but in applets we shall give complete examples. A complete and detailed list of constructors and methods for all GUI components can be found in the Java API documentation that is available on the Web.


The Checkbox Class

Applet
Relevant Java Code
public class Checklist extends Applet {

  public void init() {
    //  define checkboxes and place them vertically
    setLayout(new GridLayout(4, 1));
    add(new Checkbox("cash"));
    add(new Checkbox("credit card"));
    add(new Checkbox("passport"));
    add(new Checkbox("ticket(s)"));
  }
}
Constructors and Methods

Constructors Meaning
Checkbox() creates a checkbox with no label and no checkbox group
Checkbox(String label) creates a checkbox with the specified label but with no checkbox group
Checkbox(String label, CheckboxGroup group, boolean state) creates a checkbox with the specified label, the specified checkbox group, and the specified state
Methods Meaning
getCheckboxGroup() returns the checkbox group associated with the given checkbox
getLabel() returns the label of the checkbox
getState() returns the checkbox state: selected (true) or unselected (false)
setCheckboxGroup(CheckboxGroup group) associate the checkbox with the given checkbox group
setLabel(String label) changes the label of the checkbox
setState(boolean state) changes the checkbox state: selected (true) or unselected (false)

The CheckboxGroup Class

Applet
Relevant Java Code
public class Fontstyles extends Applet {

  public void init() {
    //  create checkbox group
    Checkboxgroup style = new Checkboxgroup();
    //  define checkboxes and
    //  place them vertically in checkbox group
    setLayout(new GridLayout(4, 1));
    add(new Checkbox("Plain", style, true));
    add(new Checkbox("Bold", style, false));
    add(new Checkbox("Italic", style, false));
    add(new Checkbox("Bold Italic", style, false));
  }
}
Constructors and Methods

Constructor Meaning
CheckboxGroup() creates a checkbox group
Methods Meaning
getCurrent() returns the current choice of checkbox in the checkbox group
setCurrent(Checkbox current) sets the current choice to the specified checkbox

The Choice Class

Applet
Relevant Java Code
public class Fontsize extends Applet {

  public void init() {
    //  create choice component
    Choice fontsize = new Choice();
    //  add items
    fontsize.addItem("10");
    fontsize.addItem("11");
    fontsize.addItem("12");
    fontsize.addItem("18");
    fontsize.addItem("20");
    fontsize.addItem("24");
    fontsize.addItem("36");
    //  add component to applet
    add(fontsize);
  }
}
Constructors and Methods

Constructor Meaning
Choice() creates a new choice component
Methods Meaning
addItem(String item) Add an item with the specified label
countItems() returns the number of items
getItem(int index) returns the item with the specified index
getSelectedIndex() returns the index of the currently selected item
getSelectedItem() returns the label of the currently selected item
select(int index) select the item at the specified index
select(String label) select the item with the specified label.

The List Class

Applet
Relevant Java Code
public class NameList extends Applet {

  public void init() {
    //  create choice component
    List names = new List(3, true);
    //  add items to list of names
    names.addItem("Alfred");
    names.addItem("Benjamin");
    names.addItem("Charles");
    names.addItem("David");
    names.addItem("Edward");
    //  add component to applet
    add(names);
  }
}
Constructors and Methods

Constructors Meaning
List() creates an empty scrolling list with no visible rows or multiple selections.
List(int numberOfRows, boolean multipleSelections) creates a scrolling list with the specified number of visible rows and a boolean stating whether multiple selections are allowed (true) or forbidden (false)
Methods Meaning
addItem(String item) Add an item with the specified label
addItem(String item, int index) Add an item with the specified label at the given position
clear() clears the list
countItems() returns the number of items
delItem(int index) deletes the item at the given position
delItems(int start, int end) deletes list items from start to end
deselect(int index) deselects the item at the given position
getItem(int index) returns the item at the given position
getRows() returns the number of visible rows in the list.
getSelectedIndex() returns the index of the currently selected item
getSelectedIndexes() returns an array of indices of currently selected items
getSelectedItem() returns the label of the currently selected item
getSelectedItems() returns an array of labels of currently selected items
replaceItem(String item, int index) replace the item at the given position
select(int index) selects the item at the given position
select(String item) selects the specified item