Lists and combo boxes are another class of interface components for user selection, used to select items in a set of selections, and combo boxes can also enter new selections.
List
A list (JList) appears as a list box in the interface and is an object of the JList class or its subclass. The program can add multiple text selection entries to the list box. There are two types of event sources for list events:
First, double-click an option with the mouse: the double-click option is an action event, the interface related to the event is ActionListener, the method to register the monitor is addActionListener(), and the interface method is actionPerformed(ActionEvent e).
The second is to click an option with the mouse: Clicking the option is an option event, and the interface related to the option event is ListSelectionListener, the method to register the monitor is addListSelectionListener, and the interface method is valueChanged(ListSelectionEvent e).
Common constructors of JList class:
Common methods of JList class:
A list can add scroll bars. The way to add scroll bars in a list is to create a list first, and then create a JScrollPane scroll panel object, specifying the list when creating a scroll panel object. The following code schematically adds a scrollbar to list2:
JScrollPane jsp = new JScrollPane(list2);
[Example] The applet has two lists. The first list only allows single selection, and the second list allows multiple selection.
import java.applet.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;class MyWindow extends JFrame implements ListSelectionL istener{ JList list1,list2; String news[]={"People Daily", "Xinmin Evening News", "Zhejiang Daily", "Wenhui Daily"}; String sports[]={"football", "volleyball", "table tennis", "basketball"}; JTextArea text; MyWindow(String s) { super(s); Container con = getContentPane(); con.setBackground(Color.BLUE); con.setLayout(new GridLayout(2,2)); con.setSize(200,500); list1 = n ew JList(news); list1.setVisibleRowCount(3); list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list1.addListSelectionListener(this); list2 = new JList(sp orts); list2.setVisibleRowCount(2); list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); list2.addListSelectionListener (this); con.add(list1); con.add(list2); text= new JTextArea(10,20); con.add(text); this.setVisible(true); this.pack(); } public void valueChanged(ListSelectionEvent e){// Call if(e.getSource()==list1){ text.setText(null); Object listValue = ((JList) e.getSour ce()). getSelectedValue(); String seleName = listValue.toString(); for(int i=0;i<news.length;i++) if(news[i].equals(seleName)){ text.append(seleN ame+ "Selected/ n"); } } else if(e.getSource()==list2){ text.setText(null); int tempList[] =list2.getSelectedIndices(); for(int i=0;i<tempList.lengt h; i++) text.append(sports[tempList[i]] + "Selected/n"); } } } public class Example6_3 extends Applet{ MyWindow myWin = new MyWindow("List Example");}Combo box
A combo box (JComboBox) is a combination of a text box and a list. You can enter options in the text box or click the drop-down button to select from the displayed list.
Common construction methods of combo boxes:
There are several other common methods for combo boxes:
Events occurring on JComboBox objects are divided into two categories. First, the user selects the project, and the event response program obtains the project selected by the user. Second, after the user enters the item, press Enter, and the event response program reads the user's input. The interface of the first type of event is ItemListener; the second type of event is the input event, and the interface is ActionListener.
[Example] An application that illustrates the usage of combo boxes. The combo box subclass declared in the program implements the ItemLister interface and the ActionListener interface. A text box and a combo box are set up in the window of the combo box subclass, and there are three options in the combo box. Implementing the monitoring method of the interface displays the selection results of the combo box in the text box.
public class Example6_4{ public static void main(String args[]){ ComboBoxDemo mycomboBoxGUI = new ComboBoxDemo(); }}class ComboBoxDemo extends JFrame implements ActionListener,ItemListener{ public static final int Width = 350; public static final int Height = 150; String proList[] = { "play football", "play basketball", "play volleyball" }; JTextField text; JComboBox comboBox; public ComboBoxDemo(){ setSize(Width,Height); setTitle ("Combo box usage diagram") ; Container conPane = getContentPane(); conPane.setBackground(Color.BLUE); conPane.setLayout(new FlowLayout()); comboBox = new JComboBox(proList) ; comboBox.addActionListener(this); combobox.addItemListener(this); comboBox .setEditable(true);//Respond to keyboard input conPane.add(comboBox); text = new JTextField(10); conPane.add(text); this.setVisible(true); } public void a ctionPerformed(ActionEvent e){ if (e.getSource()==comboBox) text.setText(comboBox.getSelectedItem().toString()); } public void itemStateChanged(ItemEvent e){ if(e.getSour ce()==comboBox){ text.setText( comboBox.getSelectedItem().toString()); } }}