Java Swing Interface Programming (30) --- list box: JList

Source: Internet
Author: User

The list box displays multiple options in a list. You can use JList to create a list box.

Package com. beyole. util; import java. awt. container; import java. awt. gridLayout; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import java. util. vector; import javax. swing. borderFactory; import javax. swing. JFrame; import javax. swing. JList; import javax. swing. listSelectionModel; class MyList {private JFrame frame = new JFrame ("Beyole"); private Container container = frame. getContentPane (); private JList list1 = null; // defines the ListBox private JList list2 = null; // defines the ListBox public MyList () {this. frame. setLayout (new GridLayout (1, 2); String nation [] = {"China", "Japan", "Russia", "North Korea", "United States"}; Vector
 
  
Vector = new Vector
  
   
(); Vector. add ("Main Site"); vector. add ("blog"); vector. add ("Forum"); this. list1 = new JList (nation); this. list2 = new JList (vector); list1.setBorder (BorderFactory. createTitledBorder ("Which country do you like"); list2.setBorder (BorderFactory. createTitledBorder ("which website do you like"); list1.setSelectionMode (ListSelectionModel. SINGLE_SELECTION); list2.setSelectionMode (ListSelectionModel. MULTIPLE_INTERVAL_SELECTION); container. add (this. list1); container. add (this. list2); this. frame. setSize (330,180); this. frame. setVisible (true); this. frame. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent arg0) {System. exit (1) ;}}) ;}} public class JListDemo01 {public static void main (String [] args) {new MyList ();}}
  
 

In the above program, the first JList sets the content of the list through a string array, and sets an option that can be selected at one time through the setSelectionMode () method, and the second JList sets the content of the list through the Vector, you can use the setSelectionMode () method to set multiple options that can be selected at a time.

Program:


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.