Java GUI programming list and combo box design using _java

Source: Internet
Author: User

Lists and combo boxes are another type of interface component for user selection, used to select a group of items, and a combo box to enter a new selection.
List

The list (JList) is displayed as a list box in the interface and 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 registration monitor method is addActionListener (), the interface method is actionperformed (ActionEvent e).
The second is the mouse click an option: Click option 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 JList classes:

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

Common methods for JList classes:

    • 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.
    • Getselectedvalue (): Gets the value of the option.
    • Getselectedindices (): Returns an array of all the selected indexes (in ascending order).
    • Getselectedvalues (),: Returns an array of all selected values sorted in ascending order according to the index in the list.
    • GetItemCount (): Gets the number of bars in the list.
    • Setvisiblerowcount (int N): Sets the number of visible rows in the list.
    • Setselectionmode (int selemode): Sets the list selection model. The selection model has two kinds of radio and multiple selection.
    • Radio: Listselectionmodel.single_selection.
    • Multiple selection: ListSelectionModel.MULTIPLE.INTERVAL_SELECTION.
    • Remove (int n): Removes the option for the specified index from the list's Options menu.
    • RemoveAll (): Deletes all the options in the list.

Lists can add scrollbars, and the list adds scrollbars by first creating a list and then creating a JScrollPane scroll panel object that specifies the list when you create a scrolling panel object. The following code illustrates adding a scrollbar to the list list2:

  JScrollPane jsp = new JScrollPane (LIST2);

The example small application has two lists, the first list allows only a single radio, and the second list allows multiple selections.

Import java.applet.*;
Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
  Class Mywindow extends JFrame implements listselectionlistener{JList;
  String news[]={"People's Daily", "Xinmin Evening News", "Zhejiang daily", "Wen Wei Hui"};
  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) {//whenever selectedCall if (E.getsource () ==list1) {text.settext (null) when an optional value changes;
      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 ("column representation Example");

Combo box

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

Common ways to construct combo boxes:

    • JComboBox (): Creates a JComboBox object with no options.
    • JComboBox (Jcomboboxmodel Amodel): Establishes a JComboBox object with the data model.
    • JComboBox (Object[]items): Creates a JComboBox object using an array object.

Other common methods for combo boxes are as follows:

    • AddItem (Object obj): Add options to combo boxes.
    • GetItemCount (): Gets the total number of entries for the combo box.
    • RemoveItem (Object ob): Deletes the specified option.
    • Removeitemat (int index): Deletes the option for the specified index.
    • Insertitemat (Object ob,int Index): Inserts the options at the specified index.
    • Getselectedindex (): Gets the index value of the selected item (starting from 0).
    • GetSelectedItem (): Gets the contents of the selected item.
    • 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 in order to respond to a select input event.

Events that occur on the JComboBox object fall into two categories. The first 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 response program reads the user's input. The interface of the first class of events is ItemListener; the second type of event is the input event, and the interface is ActionListener.

Example, an application that describes the usage of a combo box. The combined frame class declared in the program implements the Itemlister interface and the ActionListener interface. The combo box window contains a text box and a combo box with three selections. 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 using 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);//Response 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.getselectedite
  M (). toString ()); } public void ItemstatechangEd (itemevent e) {if (E.getsource () ==combobox) {Text.settext (Combobox.getselecteditem (). toString ()); }
  }
}

Related Article

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.