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: