Java know how many (88) lists and combo boxes

Source: Internet
Author: User

Lists and combo boxes are another user-selectable interface component for selecting items in a group, and the combo box can also enter a new selection.

List

A list (JList) is represented in the interface as a list box, which 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 the mouse double-click an option: The double-click option is an action event, the interface associated with the event is ActionListener, the method of registering the monitor is addActionListener (), and the interface method is actionperformed (ActionEvent e).
    • Two is the mouse click an option: Click Options is an option event, the interface associated with the option event is Listselectionlistener, the method of registering the monitor is Addlistselectionlistener, the interface method is valuechanged ( Listselectionevent e).


Common construction methods for the JList class:

    1. JList (): Create a list.
    2. JList (String list[]): Sets up a list, which is an array of strings, and the array element is the selection entry for the list.


Common methods of the JList class:

    1. Getselectedindex (): Gets the index of the option. Returns the smallest selection cell index, which is returned when only a single item in the list is selected.
    2. Getselectedvalue (): Gets the value of the option.
    3. Getselectedindices (): Returns an array of all the selected indexes (in ascending order).
    4. Getselectedvalues (): Returns an array of all selected values sorted in ascending order according to the index in their list.
    5. GetItemCount (): Gets the number of bars in the list.
    6. Setvisiblerowcount (int N): Sets the number of rows visible in the list.
    7. Setselectionmode (int selemode): Sets the list selection model. The selection model has two kinds of radio and multi-choice.
      • Radio: Listselectionmodel.single_selection.
      • Multiple selection: ListSelectionModel.MULTIPLE.INTERVAL_SELECTION.
    8. Remove (int n): Removes the option for the specified index from the list's Options menu.
    9. RemoveAll (): Removes all options from the list.


Lists can add scroll bars, and the list adds scrollbars by first creating a list and then creating a JScrollPane scrolling panel object that specifies the list when the scroll panel object is created. The following code shows the addition of scroll bars for list list2:
JScrollPane jsp = new JScrollPane (LIST2);

The "Example 11-13" applet has two lists, the first list allows only single radio, and the second list allows multiple selections.

1 Importjava.applet.*;2 Importjavax.swing.*;3 Importjava.awt.*;4 Importjava.awt.event.*;5 classMywindowextendsJFrameImplementslistselectionlistener{6 JList List1,list2;7String news[]={"People's Daily", "Xinmin Evening News", "Zhejiang daily", "Wen Wei Po"};8String sports[]={"Football", "Volleyball", "ping-pong", "basketball"};9 JTextArea text;Ten Mywindow (String s) { One         Super(s); AContainer con =Getcontentpane (); - Con.setbackground (color.blue); -Con.setlayout (NewGridLayout (2,2)); theCon.setsize (200,500); -List1 =NewJList (news); -List1.setvisiblerowcount (3); - List1.setselectionmode (listselectionmodel.single_selection); +List1.addlistselectionlistener ( This); -List2 =NewJList (sports); +List2.setvisiblerowcount (2); A List2.setselectionmode (listselectionmodel.multiple_interval_selection); atList2.addlistselectionlistener ( This); - Con.add (list1); - Con.add (list2); -text=NewJTextArea (10,20); - Con.add (text); -          This. setvisible (true); in          This. Pack (); -     } to      Public voidValueChanged (Listselectionevent e) {//called whenever a value change is selected +         if(E.getsource () = =List1) { -Text.settext (NULL); theObject ListValue =( (JList) E.getsource ()). Getselectedvalue (); *String Selename =listvalue.tostring (); $              for(inti=0;i<news.length;i++)Panax Notoginseng                 if(News[i].equals (selename)) { -Text.append (selename+ "is selected \ n"); the                 } +         } A         Else if(E.getsource () = =List2) { theText.settext (NULL); +             intTemplist[] =list2.getselectedindices (); -              for(inti=0;i<templist.length;i++) $Text.append (Sports[templist[i]] + "Selected \ n"); $          } -      } -  } the  Public classExample6_3extendsapplet{ -Mywindow Mywin =NewMywindow ("list Example");Wuyi}
Combo box

A combo box (JCOMBOBOX) is a combination of a text box and a list that you can enter in the text box, or you can click the drop-down button to select from the list that appears.

Common construction methods for combo boxes:

    1. JComboBox (): Set up a JComboBox object with no options.
    2. JComboBox (Jcomboboxmodel AModel): Establishes a JComboBox object with the data model.
    3. JComboBox (Object[]items): Creates a JComboBox object with an array object.


The other common methods of a combo box are the following:

    1. AddItem (Object obj): Adds an option to the combo box.
    2. GetItemCount (): Gets the total number of entries for the combo box.
    3. RemoveItem (Object ob): Deletes the specified option.
    4. Removeitemat (int index): Removes the option for the specified index.
    5. Insertitemat (Object ob,int Index): Inserts an entry at the specified index.
    6. Getselectedindex (): Gets the index value of the selected item (starting at 0).
    7. GetSelectedItem (): Gets the contents of the selected item.
    8. Seteditable (Boolean B): set to editable. The default state of a combo box is not editable, and you need to call this method to be editable to respond to the select input event.


There are two types of events that occur on JComboBox objects. One is the user selected item, and the event responder gets the item selected by the user. Second, the user enters the item and presses the ENTER key, and the event responder reads the user's input. The interface for the first type of event is itemlistener; the second type of event is the input event, and the interface is ActionListener.

Example 11-14 an application that describes the use of a combo box. The combo box class declared in the program implements the Itemlister interface and the ActionListener interface. A text box and a combo box are set in the window of the combo box class, and there are three choices in the ComboBox. Implements the monitoring method for an interface to display the selection results of a combo box in a text box.

1  Public classexample6_4{2      Public Static voidMain (String args[]) {3Comboboxdemo Mycomboboxgui =NewComboboxdemo ();4     }5 }6 classComboboxdemoextendsJFrameImplementsactionlistener,itemlistener{7      Public Static Final intWidth = 350;8      Public Static Final intHeight = 150;9String prolist[] = {"Play football", "Play Basketball", "play volleyball" };Ten JTextField text; One JComboBox ComboBox; A      PublicComboboxdemo () { - setSize (width,height); -Settitle ("combo box using schematic program"); theContainer Conpane =Getcontentpane (); - Conpane.setbackground (color.blue); -Conpane.setlayout (NewFlowLayout ()); -ComboBox =NewJComboBox (prolist); +Combobox.addactionlistener ( This); -Combobox.additemlistener ( This); +Combobox.seteditable (true);//responding to keyboard input A Conpane.add (comboBox); atText =NewJTextField (10); - Conpane.add (text); -          This. setvisible (true); -     } -      Public voidactionperformed (ActionEvent e) { -         if(E.getsource () = =ComboBox) in Text.settext (Combobox.getselecteditem (). toString ()); -     } to      Public voiditemstatechanged (itemevent e) { +         if(E.getsource () = =ComboBox) { - Text.settext (Combobox.getselecteditem (). toString ()); the         } *     } $}

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttons

Java know how many (88) lists and combo boxes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.