Use of list box Space JList Demo

Source: Internet
Author: User

Package list box control demo; import Java.awt.color;import Java.awt.gridlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;import Javax.swing.borderfactory;import Javax.swing.defaultlistmodel;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.jlabel;import Javax.swing.jlist;import Javax.swing.jpanel;import Javax.swing.listselectionmodel;public class MyFrame extends JFrame implements ActionListener {// The following is the private variable of Defaultlistmodel, private Defaultlistmodel sourcemodel;private Defaultlistmodel destmodel;//  Create a variable of two JList type private JList source;//has not been initialized private JList dest = new JList ();//Initialize//Create two button objects private JButton AddButton = new JButton (">>");p rivate JButton RemoveButton = new JButton ("<<");//constructor with parameters public MyFrame (String title) { Super (title);//The constructor that passes this string to the parent class is displayed in the title bar of the form//Fortunately the designer of the swing API came up with an adapter adapter class for event handling, which eliminates the hassle of implementing many interface methods, The following is an anonymous inner class Addwindowlistener (new Windowadapter () {public void windOwclosing (WindowEvent e) {//implement this method to gracefully terminate the program System.exit (0);}); /Create an instance of the Dufaultlistmodel class Sourcemodel = new Defaultlistmodel ();//Add several string-type options to the created data Model sourcemodel.addelement (" Banana "), Sourcemodel.addelement (" Apple "), Sourcemodel.addelement (" Orange "); Sourcemodel.addelement (" Mango "); Sourcemodel.addelement ("Pineapple"); Sourcemodel.addelement ("Kiwi"); Sourcemodel.addelement ("Strawberry"); Sourcemodel.addelement ("Peach");//Create a JList instance and pass the previously created data model as a parameter to the JList constructor Source = new JList (Sourcemodel);// This is not previously instantiated//to set the selection mode for the source list box: Radio mode Source.setselectionmode (listselectionmodel.single_selection);//source.setborder ( Borderfactory.createtitledborder (Borderfactory.createlineborder (color.dark_gray,5), "" "+" Shop ", 0,0,null, color.red));//Set the first option selected in the Initialize list box Source.setselectedindex (0); Source.setselectionbackground (Color.Black); Source.setselectionforeground (Color.White);//Create another list box's data Model Destmodel = new Defaultlistmodel ();d Est.setmodel ( Destmodel);//This is the Dest.setselectionbackground (Color.Black);d that created the instance object previously Est.setselectionforeGround (Color.White);d Est.setborder (Borderfactory.createtitledborder (Borderfactory.createlineborder (Color.DARK_ gray,5), "" + "Fruit basket", 0,0,null,color.red);//tip: Create a data model that allows you to replace the contents of a list box while the program is running//build GUI: Creation of user interface// The purpose of the panel is to be used as a container for placing components JPanel panel = new JPanel ();//For Panel settings layout Manager for grid layout 4 row 1 column panel.setlayout (new GridLayout (4,1,20,20)); Panel.add (New JLabel ());//1th Act empty is the label Panel.add (AddButton);p anel.add (RemoveButton);p Anel.add (New JLabel ());// The last action empty is a label to display on the panel//for the window set layout manager for Grid layout 1 row 3 column this.setlayout (new GridLayout (1,3,20,20)); Add (source); Add (Panel); Add ( dest);//Register Event Listener: This is the last thing to do, which is to add an event listener Addbutton.addactionlistener (this) to the button; Removebutton.addactionlistener ( this);} The following is the event handling code for the button @overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method Stubif (E.getsource (). Equals (AddButton)) {if (Source.getselectedvalue ()!=null)//calls the Getselectedvalue () method of the JList class {string str= (String) Source.getselectedvalue (); if (str!=null) {destmodel.addelement (str);d est.setselectedindex (0); Sourcemodel.removeelement (str); Source.setselEctedindex (0);}} if (E.getsource (). Equals (RemoveButton)) {if (Dest.getselectedvalue ()!=null) {string str= (String) Dest.getselectedvalue (); if (str!=null) {sourcemodel.addelement (str); Source.setselectedindex (0); Destmodel.removeelement (str);d est.setselectedindex (0);}}}

Here is the class of the test:

Package list box control demo; public class Listdemoapp {public static void main (string[] args) {//TODO auto-generated method Stubmyfram E frame = new MyFrame ("List Demo"); Frame.setbounds (20,50,400,300);//(X,y,width,heigth)//frame.setsize (400,300);( Width,heigth) frame.setvisible (True);}}


Use of list box Space JList Demo

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.