Lists and combo boxes are another type of interface component for users to choose from.Used to select items in a group,The combo box can also enter new selections.


The list (jlist) appears as a list box in the interface,Is an object of the jlist class or its subclasses.The program can include multiple text selection entries in the list box.There are two types of event sources for list events:

One is to double-click an option:the double-click option is an action event,The interface related to this event is actionlistener, the method for registering the monitor is addactionlistener (), and the interface method is actionperformed (actionevent e).

The second is a mouse click on an option:a click option is an option event,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 construction methods of the jlist class:

jlist ():build a list. jlist (string list []):build a list,list is an array of strings,The array elements are the select items of the list.

Common methods of the jlist class:

getselectedindex ():Get the index of the selected item.Returns the smallest selection unit index;When only a single item in the list is selected,Go back to that selection. getselectedvalue ():Get the value of the option. getselectedindices ():Returns an array of all selected indexes (in ascending order). getselectedvalues ​​() ,:returns an array of all selected values,Sort in ascending order according to the order of the indexes in their list. getitemcount ():Get the number of items in the list. setvisiblerowcount (int n):Set the number of visible rows in the list. setselectionmode (int selemode):Set the list selection model.There are two types of selection models:single selection and multiple selection. Single selection:listselectionmodel.single_selection. Multiple selection:listselectionmodel.multiple.interval_selection. remove (int n):Removes the specified indexed option from the list's option menu. removeall ():Removes all options from the list.

Lists can add scroll bars,The way to add a scroll bar to a list is to first create the list,Then create a jscrollpane scroll panel object,Specify a list when creating a scroll panel object.The following code illustrates adding a scroll bar to list2:

 jscrollpane jsp=new jscrollpane (list2);

[Example] The applet has two lists,The first list only allows radio selection,The second list allows multiple selection.

import java.applet. *;
import javax.swing. *;
import java.awt. *;
import java.awt.event. *;
class mywindow extends jframe implements listselectionlistener {
  jlist list1, list2;
  string news []={"People's 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=new jlist (news);
    list1.setvisiblerowcount (3);
    list1.setselectionmode (listselectionmodel.single_selection);
    list1.addlistselectionlistener (this);
    list2=new jlist (sports);
    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) {//Called whenever the selection value changes
    if (e.getsource () == list1) {
      text.settext (null);
      object listvalue=((jlist) e.getsource ()). getselectedvalue ();
      string selename=listvalue.tostring ();
      for (int i=0;i<news.length;i ++)
        if (news [i] .equals (selename)) {
          text.append (selename + "Selected \ n");
    else if (e.getsource () == list2) {
      text.settext (null);
      int templist []=list2.getselectedindices ();
      for (int i=0;i<templist.length;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,You can also click the drop-down button to select from the displayed list.

Common construction methods of combo boxes:

jcombobox ():Create a jcombobox object without options. jcombobox (jcomboboxmodel amodel):Use the data model to create a jcombobox object. jcombobox (object [] items):Creates a jcombobox object using an array object.

Other common methods of combo boxes are the following:

additem (object obj):Add options to the combo box. getitemcount ():Get the total number of entries in the combo box. removeitem (object ob):delete the specified option. removeitemat (int index):remove the option of the specified index. insertitemat (object ob, int index):inserts an option at the specified index. getselectedindex ():Get the index value of the selected item (starting from 0). getselecteditem ():Get the content of the selected item. seteditable (boolean b):Set to editable.The default state of the combo box is not editable.Need to call this method to be editable,To respond to the selection input event.

There are two types of events that occur on the jcombobox object.One is the item selected by the user,The incident responder gets the item selected by the user.Second, the user presses the enter key after entering the item,The event responder reads user input.The interface of the first type of event is itemlistener;the second type of event is the input event,The interface is actionlistener.

[Example] An application that illustrates the use of combo boxes.The combo box subclass declared in the program implements the itemlister interface and the actionlistener interface. A combo box subclass window has a text box and a combo box.There are three choices in the combo box.The monitoring method that implements the interface displays the selection result 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 uses a schematic program");
    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 actionperformed (actionevent e) {
    if (e.getsource () == combobox)
      text.settext (combobox.getselecteditem (). tostring ());
  public void itemstatechanged (itemevent e) {
    if (e.getsource () == combobox) {
      text.settext (combobox.getselecteditem (). tostring ());
  • Previous php + ajax implement input read database display matching information
  • Next In-depth analysis of the creation and deletion of Oracle database management database
  • Trends