Java Interface Programming (9)------list box

Source: Internet
Author: User

This article is my own study notes, welcome reprint. But please specify the source:http://blog.csdn.net/jesson20121020

The list box and the JComboBox combo box are significantly different, which is not just the body's appearance today.

When the JComboBox combo box is activated, a drop-down list appears, and JList always occupies a fixed line of space on the screen, and the size does not change.

Suppose you want to get the selected item in the list box, just call Getselectvalues (), which can produce an array of strings. Inside is the selected item name.

Other than that. JList components also agree to multiple selections, if you hold down the CTRL key. Click on multiple items consecutively, then the previously selected item remains selected, that is, you can select as many items as you like, if you select an item, Shift-click, and a project, all items between the two items will be selected. This is similar to the action Check operation under Windows.

public class List extends JFrame {private string[] weekdays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};p rivate Defaultlistmodel litems = new Defaultlistmodel ();p rivate JList lst = new JList (litems);p rivate jtextarea t = new JTextArea (weekdays.length,10);p rivate JButton b = new JButton ("Add Items");p rivate int count = 0;private ActionListener bl = new Ac Tionlistener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method Stubif (Count < week Days.length) {litems.add (0, weekdays[count++]);} Else{b.setenabled (FALSE);}}; Private Listselectionlistener LL = new Listselectionlistener () {@Overridepublic void valuechanged (Listselectionevent e) {//TODO auto-generated method Stubif (e.getvalueisadjusting ()) return; T.settext (""); for (Object item: Lst.getselectedvalues ()) T.append (item + "\ n");}; Public List () {//TODO auto-generated constructor Stubsetlayout (new FlowLayout ()); setvisible (true); SetSize (300,200); T.setenabled (FALSE); Border BRD = Borderfactory.creatematteBorder (1, 1, 2, 2, Color.Black); Lst.setborder (BRD); T.setborder (BRD); for (int i = 0; i < 4;i++) {litems.addelement ( weekdays[count++]);} Add (t); add (LST); add (b); Lst.addlistselectionlistener (ll); B.addactionlistener (BL);} /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubnew List ();}}
Running results such as the following:

In fact, this list box can be seen in very many applications. Also, when there are too many items in the list box, JList does not provide scrolling and, of course, can wrap the jlist to JScrollPane, which will result in scrolling.



Java Interface Programming (9)------list box

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.