Control Mechanisms Lists Comboboxes Spinners List Controls A
Control Mechanisms: Lists, Comboboxes, Spinners
List Controls A JList has the following four possible constructors: JList(List. Model) JList(Object[]) JList(Vector) JList() Create a list with the initial list items specified. The second and third constructors implicitly create an immutable List. Model; you should not subsequently modify the passed-in array or Vector.
List Controls A JList presents the user with a group of items, displayed in one or more columns, to choose from. Lists can have many items, so they are often put in scroll panes. Default. List. Model list. Model; list. Model = new Default. List. Model(); list. Model. add. Element("A"); list. Model. add. Element("B"); list. Model. add. Element("C"); list. Model. add. Element("D"); list. Model. add. Element("E");
List Controls A JList presents the user with a group of items, displayed in one or more columns, to choose from. Lists can have many items, so they are often put in scroll panes. JList list; list = new JList(list. Model); list. set. Selection. Mode (List. Selection. Model. SINGLE_SELECTION); list. add. List. Selection. Listener(this); list. set. Visible. Row. Count(4); JScroll. Pane list. Scroll. Pane = new JScroll. Pane(list); this. add(list. Scroll. Pane, c);
List Controls A list uses an instance of List. Selection. Model to manage its selection. You can specify a different selection mode by calling the set. Selection. Mode method on the list. SINGLE_SELECTION - Only one item can be selected at a time. When the user selects an item, any previously selected item is deselected first. SINGLE_INTERVAL_SELECTION - Multiple, contiguous items can be selected. When the user begins a new selection range, any previously selected items are deselected first. MULTIPLE_INTERVAL_SELECTION - The default. Any combination of items can be selected. The user must explicitly deselect items.
List Controls No matter which selection mode your list uses, the list fires list selection events whenever the selection changes. You can process these events by adding a list selection listener to the list with the add. List. Selection. Listener method. A list selection listener must implement one method: value. Changed. //This method is required by List. Selection. Listener. public void value. Changed(List. Selection. Event e){ if (e. get. Value. Is. Adjusting() == false) { //Selection, enable the fire button. Set. Backgr. set. Enabled(true); Set. Foregr. set. Enabled(true); } }
Combo. Box Controls A JCombo. Box , which lets the user choose one of several choices, can have two very different forms. uneditable combo box: features a button and a drop-down list of values. (default form) editable combo box: features a text field with a small button abutting it. The user can type a value in the text field or click the button to display a drop-down list.
Combo. Box Controls The JCombo. Box has four constructors: JCombo. Box() JCombo. Box(Combo. Box. Model) JCombo. Box(Object[]) JCombo. Box(Vector) Create a combo box with the specified items in its menu. A combo box created with the default constructor has no items in the menu initially. Each of the other constructors initializes the menu from its argument: a model object, an array of objects, or a Vector of objects.
Combo. Box Controls The following code, taken from Combo. Box. Demo. java , creates an uneditable combo box and sets it up: String[] pet. Strings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; //Create the combo box, select item at index 4. //Indices start at 0, so 4 specifies the pig. JCombo. Box pet. List = new JCombo. Box(pet. Strings); pet. List. set. Selected. Index(4); pet. List. add. Action. Listener(this);
Combo. Box Controls Two types of event listeners are commonly written for the Combo. Box: Action. Listeners and Item. Listeners A combo box fires an action event when the user selects an item from the combo box's menu. A combo box fires two item events when any of the items' selection state changes (one to select the new item, one to deselect the previous item. ) If the user chooses the same item, no item events are fired.
Combo. Box Controls Here's the code from Combo. Box. Demo. java that registers and implements an action listener on the combo box: public class Combo. Box. Demo. . . implements Action. Listener {. . . pet. List. add. Action. Listener(this) {. . . public void action. Performed(Action. Event e) { JCombo. Box cb = (JCombo. Box)e. get. Source(); String pet. Name = (String)cb. get. Selected. Item(); update. Label(pet. Name); }. . . }
Spinner Controls Spinners are similar to combo boxes and lists in that they let the user choose one from a range of values. Spinners: - allow the user to type in a value - don't have a drop-down list that can cover up other components. - don't display possible values often used when the set of possible values is extremely large. - should only be used when the possible values and their sequence are obvious.
Spinner Controls A spinner is a compound component with three subcomponents: two small buttons and an editor. The editor can be any JComponent, but by default is implemented as a panel that contains a formatted text field. The spinner's possible and current values are managed by its model.
Spinner Controls There are two constructors for the JSpinner: JSpinner() JSpinner(Spinner. Model) Create a new JSpinner. The no-argument constructor creates a spinner with an integer Spinner. Number. Model with an initial value of 0 and no minimum or maximum limits. The optional parameter on the second constructor allows you to specify your own Spinner. Model.
Spinner Controls The Swing API provides three spinner models: Spinner. Number. Model Spinner. List. Model Spinner. Date. Model Spinner. Number. Model - Supports sequences of numbers, which can be expressed as doubles, ints, or Numbers. You can specify the minimum and maximum allowable values, as well as the step size — the amount of each increment or decrement. The Year spinner uses this model, created with the following code: Spinner. Model model = new Spinner. Number. Model(current. Year, //initial value current. Year - 100, //min current. Year + 100, //max 1); //step
Spinner Controls Spinner. List. Model A spinner model whose values are defined by an array of objects or a List. Spinner. Demo's Month spinner uses this model, initialized with an array derived from the value returned by the get. Months method of java. text. Date. Format. Symbols. Spinner. Date. Model Supports sequences of Date objects. You can specify the minimum and maximum dates, as well as the field (such as Calendar. YEAR) to increment or decrement. Note, however, that some look and feels ignore the specified field, and instead change the field that appears selected.
Spinner Controls You can detect that a spinner's value has changed by registering a change listener on either the spinner or its model. public class Spinner. Demo 3 extends JPanel implements Change. Listener { protected JSpinner date. Spinner; . . . date. Spinner. add. Change. Listener(this); . . . public void state. Changed(Change. Event e) { Spinner. Model date. Model = date. Spinner. get. Model(); if (date. Model instanceof Spinner. Date. Model) { set. Seasonal. Color( ((Spinner. Date. Model)date. Model). get. Date( )); } }. . .
- Slides: 17