Chapter Topics Chapter 13 discusses the following main topics: – File Choosers and Color Choosers – Menus – More about Text Components: Text Areas and Fonts – Sliders – Look and Feel
Read Only Text Fields • Read only text fields are a different way to use the JTextField component. • The JTextField component has a method named setEditable: setEditable(boolean editable)
• By default a text field is editable.
• The setEditable method must be called and passed false to make the field read-only.
Lists • A list is a component that displays a list of items and allows the user to select items from the list. • The JList component is used for creating lists. • When an instance of the JList class is created, an array of objects is passed to the constructor. JList (Object[] array) • The JList component uses the array to create the list of items. String[] names = { "Bill", "Geri", "Greg", "Jean", "Kirk", "Phillip", "Susan" }; JList nameList = new JList(names);
List Selection Modes • The JList component can operate in any of the following selection modes: – Single Selection Mode - Only one item can be selected at a time. – Single Interval Selection Mode - Multiple items can be selected, but they must be in a single interval. An interval is a set of contiguous items. – Multiple Interval Selection Mode - In this mode multiple items may be selected with no restrictions. • This is the default selection mode.
List Selection Modes • You change a JList component’s selection mode with the setSelectionMode method. • The method accepts an int argument that determines the selection mode: – ListSelectionModel.SINGLE_SELECTION – ListSelectionModel.SINGLE_INTERVAL_SELECTION – ListSelectionModel.MULTIPLE_INTERVAL_SELECTION • Example: nameList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
List Events • When an item in a JList object is selected it generates a list selection event. • The event is handled by an instance of a list selection listener class, which must meet the following requirements: – It must implement the ListSelectionListener interface. – It must have a method named valueChanged. This method must take an argument of the ListSelectionEvent type. • Use the addListSelectionListener method of the JList class to register the instance of the list selection listener class with the list object.
List Events • When the JList component generates an event: – It automatically executes the valueChanged method of the list selection listener object – It passes the event object as an argument.
Retrieving Selected Items • You may use: • getSelectedValue or • getSelectedIndex – to determine which item in a list is currently selected. • getSelectedValue returns a reference to the item that is currently selected. String selectedName; selectedName = (String)nameList.getSelectedValue();
• The return value must be cast to String is required in order to
store it in the selectedName variable. • If no item in the list is selected, the method returns null.
Retrieving Selected Items • The getSelectedIndex method returns the index of the selected item, or –1 if no item is selected. • Internally, the items that are stored in a list are numbered (similar to an array). • Each item’s number is called its index. • The first item has the index 0. • You can use the index of the selected item to retrieve the item from an array. String[] names = { "Bill", "Geri", "Greg", "Jean", "Kirk", "Phillip", "Susan" }; JList nameList = new JList(names);
Retrieving Selected Items • This code could be used to determine the selected item: int index; String selectedName; index = nameList.getSelectedIndex(); if (index != -1) selectedName = names[index];
Adding A Scroll Bar To a List • By default, a list component is large enough to display all of the items it contains. • Sometimes a list component contains too many items to be displayed at once. • Most GUI applications display a scroll bar on list components that contain a large number of items. • List components do not automatically display a scroll bar.
Adding A Scroll Bar To a List • To display a scroll bar on a list component, follow these general steps. 1. Set the number of visible rows for the list component. 2. Create a scroll pane object and add the list component to it. 3. Add the scroll pane object to any other containers, such as panels. • For this list: String[] names = { "Bill", "Geri", "Greg", "Jean", "Kirk", "Phillip", "Susan" }; JList nameList = new JList(names);
Adding A Scroll Bar To a List • Establish the size of the list component. nameList.setVisibleRowCount(3); • Create a scroll pane object and add the list component to it. • A scroll pane object is a container that displays scroll bars on any component it contains. • The JScrollPane class to create a scroll pane object. • We pass the object that we wish to add to the scroll pane as an argument to the JScrollPane constructor. JScrollPane scrollPane = new JScrollPane(nameList);
Adding A Scroll Bar To a List • Add the scroll pane object to any other containers that are necessary for our GUI. JPanel panel = new JPanel(); panel.add(scrollPane); add(panel); • When the list component is displayed, it will appear with: – Three items showing at a time and – scroll bars:
Adding A Scroll Bar To a List • By default, JList components added to a JScrollPane object only display a scroll bar if there are more items in the list than there are visible rows. • When a JList component is added to a JScrollPane object, a border will automatically appear around the list. • Example: ListWindowWithScroll.java
Single Interval Selection Mode • A list is set to single interval selection mode by passing the constant ListSelectionModel.SINGLE_INTERVAL_SELECTION to the component’s setSelectionMode method. • An interval is a set of contiguous items. • The user selects: – the first item in the interval by clicking on it – the last item by holding the Shift key while clicking on it. • All of the items that appear in the list from the first item through the last item are selected.
Single Interval Selection Mode • The getSelectedValue method returns the first item in the selected interval. • The getSelectedIndex method returns the index of the first item in the selected interval. • To get the entire selected interval, use the getSelectedValues method. – This method returns an array of objects, which are the items in the selected interval. • The getSelectedIndices method returns an array of int values that are the indices of all the selected items in the list.
Multiple Interval Selection Mode • Set multiple interval selection mode by passing the constant ListSelectionModel.MULTIPLE_INTERVAL_SELECTION to the component’s setSelectionMode method. • In multiple interval selection mode: – multiple items can be selected – the items do not have to be in the same interval. • In multiple interval selection mode the user can select single items or intervals.
Multiple Interval Selection Mode • The user holds down the Ctrl key while clicking on an item – it selects the item without deselecting other items. • The getSelectedValue method returns the first selected item. • The getSelectedIndex method returns the index of the first selected item. • The getSelectedValues method returns an array of objects containing the items that are selected. List<E> getSelectedValuesList • The int[] getSelectedIndices method returns an int array containing the indices of the selected items.
Combo Boxes • A combo box presents a drop-down list of items that the user may select from. • The JComboBox class is used to create a combo box. • Pass an array of objects that are to be displayed as the items in the drop-down list to the constructor. String[] names = { "Bill", "Geri", "Greg", "Jean", "Kirk", "Phillip", "Susan" }; JComboBox nameBox = new JComboBox(names); • When displayed, the combo box created by this code will initially appear as the button:
Combo Boxes • The button displays the item that is currently selected. • The first item in the list is automatically selected when the combo box is displayed. • When the user clicks on the button, the drop- down list appears and the user may select another item.
Combo Box Events • When an item in a JComboBox object is selected, it generates an action event. • Handle action events with an action event listener class, which must have an actionPerformed method. • When the user selects an item in a combo box, the combo box executes its action event listener’s actionPerformed method, passing an ActionEvent object as an argument.
Retrieving Selected Items • There are two methods in the JComboBox class that can be used to determine which item in a list is currently selected: – getSelectedItem – getSelectedIndex • The getSelectedItem method returns a reference to the item that is currently selected. String selectedName; selectedName = (String) nameBox.getSelectedItem();
• getSelectedItem returns an Object reference so we cast
Editable Combo Boxes • There are two types of combo boxes: – uneditable – allows the user to only select items from its list. – editable – combines a text field and a list. • It allows the selection of items from the list • allows the user to type input into the text field • The setEditable method sets the edit mode for the component. String[] names = { "Bill", "Geri", "Greg", "Jean", "Kirk", "Phillip", "Susan" }; JComboBox nameBox = new JComboBox(names); nameBox.setEditable(true);
Editable Combo Boxes • An editable combo box appears as a text field with a small button displaying an arrow joining it. • When the user clicks on the button, the drop-down list appears as shown in the center of the figure. • The user may: – select an item from the list. – type a value into the text field. • The user is not restricted to the values that appear in the list, and may type any input into the text field.