A list box can also be used to create a list box when information is presented to the user's list of multiple options, using JList.
Package Com.beyole.util;import Java.awt.container;import Java.awt.gridlayout;import java.awt.event.WindowAdapter; Import Java.awt.event.windowevent;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");p rivate Container Container = Frame.getcontentpane ();p rivate JList list1 = null;//define list box private JLI St List2 = null;//defines the list box public MyList () {this.frame.setLayout (new GridLayout (1, 2)); String nation[] = {"China", "Japan", "Russia", "North Korea", "USA"}; vector<string> vector = new vector<string> (); Vector.add ("Master"); 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 site 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 (true); This.frame.addWindowListener (new Windowadapter () {public void windowclosing (WindowEvent arg0) {system.exit (1) ;}});}} public class JListDemo01 {public static void main (string[] args) {new MyList ();}}
The first jlist in the above program sets the contents of the list through a string array, and the Setselectionmode () method sets one option that can be selected at a time, and the second jlist sets the contents of the list through a vector. and set multiple options that can be selected at once using the Setselectionmode () method
Program:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehvlamlhd2vpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Java Swing Programming Interface (---) list box: JList