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

Source: Internet
Author: User

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

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.