Today we will focus on JSpinner and JList. JSpinner is rarely used. Generally, it is replaced by a scroll bar, but when the value is precise, it is embarrassing to use a scroll bar to often fail to get its own value, then JSpinner will be used.
In fact, JSpinner has no tricks. During the main construction, a SpinnerModel should be passed. This class has three subclasses.
The SpinnerNumberModel is used to set the numeric JSpinner.
The SpinnerDateModel is used to fine-tune the time control.
The SpinnerListModel is used to fine-tune the input between lists or arrays. For example
Public class TestJSpinner <br/>{< br/> final int SPINNER_NUM = 6; <br/> JFrame mainWin = new JFrame ("fine-tuning controller demonstration "); <br/> Box spinnerBox = new Box (BoxLayout. y_AXIS); <br/> JSpinner [] spinners = new JSpinner [SPINNER_NUM]; <br/> JLabel [] valLabels = new JLabel [SPINNER_NUM]; <br/> JButton okBn = new JButton ("OK"); <br/> public void init () <br/>{< br/> for (int I = 0; I <SPINNER_NUM; I ++) <br/>{< br/> valLabels [I] = new JLabel (); <br/>}< br/> // ----------- normal Spinner --------- <br/> spinners [0] = new JSpinner (); <br/> addSpinner (spinners [0], "normal", valLabels [0]); </p> <p> // ----------- specify the minimum value, maximum value, and step size of the Spinner ----------- <br/> // create a SpinnerNumberModel object, specify the minimum value, maximum value, and step size <br/> SpinnerNumberModel numModel = new SpinnerNumberModel (3.4, <br/>-1.1, 4.3, 0.1 ); <br/> spinners [1] = new JSpinner (numModel); <br/> addSpinner (spinners [1], "number range", valLabels [1]); </p> <p> // ----------- use the Spinner of the SpinnerListModel ------------ <br/> String [] books = new String [] <br/>{< br/> "Lightweight J2EE enterprise Application practices ", <br/> "Struts2 authoritative guide", <br/> "J2EE-based Ajax" <br/> }; <br/> // create a SpinnerListModel object using a string array <br/> SpinnerListModel bookModel = new SpinnerListModel (books ); <br/> // use the SpinnerListModel object to create a JSpinner object <br/> spinners [2] = new JSpinner (bookModel); <br/> addSpinner (Spinners [2], "string Sequence Value", valLabels [2]); </p> <p> // ----------- use a Sequence Value of the image ICON's Spinner ------------ <br/> ArrayList <ImageIcon> icons = new ArrayList <ImageIcon> (); <br/> icons. add (new ImageIcon ("a.gif"); <br/> icons. add (new ImageIcon ("B .gif"); <br/> // create a SpinnerListModel object using the ImageIcon array <br/> SpinnerListModel iconModel = new SpinnerListModel (icons ); <br/> // use the SpinnerListModel object to create a JSpinner object <br/> spinners [3] = New JSpinner (iconModel); <br/> addSpinner (spinners [3], "icon Sequence Value", valLabels [3]); </p> <p> // ----------- use the Spinner ------------ of the SpinnerDateModel <br/> // obtain the start time, end time, and initial time respectively. <br/> Calendar cal = Calendar. getInstance (); <br/> Date init = cal. getTime (); <br/> cal. add (Calendar. DAY_OF_MONTH,-3); <br/> Date start = cal. getTime (); <br/> cal. add (Calendar. DAY_OF_MONTH, 8); <br/> Date end = cal. getTime (); <br/> // create an Spi NnerDateModel object, specifying the minimum time, maximum time, and initial time <br/> SpinnerDateModel dateModel = new SpinnerDateModel (init, <br/> start, end, Calendar. HOUR_OF_DAY); <br/> // create a JSpinner with a SpinnerDateModel object <br/> spinners [4] = new JSpinner (dateModel); <br/> addSpinner (spinners [4], "Time Range", valLabels [4]); </p> <p> // ----------- format the Spinner ------------ using DateEditor <br/> dateModel = new SpinnerDateModel (); <br/> spinners [5] = new JSpinne R (dateModel); <br/> // create a JSpinner. dateEditor object, used to format the specified Spinner <br/> JSpinner. dateEditor editor = new JSpinner. dateEditor (spinners [5], <br/> "HH, MM dd, yyyy"); <br/> // you can use JSpinner. format the DateEditor object <br/> spinners [5]. setEditor (editor); <br/> addSpinner (spinners [5], "Use DateEditor", valLabels [5]); <br/> // Add an event listener for the "OK" button <br/> okBn. addActionListener (new ActionListener () <br/>{< br/> public void actio Nshortmed (ActionEvent evt) <br/>{< br/> // retrieves the value of each fine-tuning controller and displays the value with the Label. <Br/> for (int I = 0; I <SPINNER_NUM; I ++) <br/> {<br/> valLabels [I]. setText (spinners [I]. getValue (). toString (); <br/>}< br/>}); </p> <p> JPanel bnPanel = new JPanel (); <br/> bnPanel. add (okBn); <br/> mainWin. add (spinnerBox, BorderLayout. CENTER); <br/> mainWin. add (bnPanel, BorderLayout. SOUTH); <br/> mainWin. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); <br/> mainWin. pack (); <br/> mainWin. setVisible (true); </p> <p >}< br/> // defines a method, used to add slide bars to the container <br/> public void addSpinner (JSpinner spinner, String description <br/>, JLabel valLabel) <br/>{< br/> Box box Box = new Box (BoxLayout. x_AXIS); <br/> JLabel desc = new JLabel (description + ":"); <br/> desc. setPreferredSize (new Dimension (100, 30); <br/> box. add (desc); <br/> box. add (spinner); <br/> valLabel. setPreferredSize (new Dimension (180, 30); <br/> box. add (valLabel); <br/> spinnerBox. add (box); <br/>}< br/> public static void main (String [] args) <br/>{< br/> new TestJSpinner (). init (); <br/>}< br/>
Compared with the previous Jspinner, JList and JComboBox are used more.
Public class testlist <br/>{< br/> private jframe mainwin = new jframe ("test list box "); <br/> string [] books = new string [] <br/> {<br/> "spring2.0 baodian", <br/> "Lightweight J2EE Enterprise Application Practice ", <br/> "J2EE-based Ajax book", <br/> "struts2 authoritative guide", <br/> "ror best practices for agile development" <br/> }; <br/> jlist booklist = new jlist (books); <br/> jcombobox bookselector; <br/> // define the panel where the layout selection button is located <br/> jpanel layoutpanel = new jpanel (); <br/> buttongroup layoutgroup = new buttongroup (); <br/> // define the panel where the select mode button is located <br/> jpanel selectmodepanel = new jpanel (); <br/> buttongroup selectmodegroup = new buttongroup (); <br/> jtextarea favoriate = new jtextarea (4, 40); </P> <p> Public void Init () <br/>{< br/> // The visual height of jlist can display three list items at the same time <br/> Booklist. setvisiblerowcount (3); <br/> // by default, the third to fifth items are selected (the first index is 0) <br/> Booklist. setselectioninterval (2, 4); <br/> addlayoutbutton ("vertical scroll", jlist. vertical); <br/> addlayoutbutton ("vertical line feed", jlist. vertical_wrap); <br/> addlayoutbutton ("horizontal line feed", jlist. horizontal_wrap); <br/> addselectmodelbutton ("unrestricted", listselectionmodel. multiple_interval_selection); <br/> addselectmodelbutton ("single choice", listselectionmodel. single_selection); <br/> addselectmodelbutton ("single range", listselectionmodel. single_interval_selection); <br/> box ListBox = new box (boxlayout. y_axis); <br/> // place the jlist component in jscrollpane and add the jscrollpane To The ListBox container. <br/> ListBox. add (New jscrollpane (booklist); <br/> // Add layout selection button panel and select mode button panel <br/> ListBox. add (layoutpanel); <br/> ListBox. add (selectmodepanel); <br/> // Add event listeners to jlist <br/> Booklist. addlistselectionlistener (New listselectionlistener () <br/>{< br/> Public void valuechanged (listselectionevent E) <br/>{< br/> // obtain all the books selected by the user <br/> object [] books = Booklist. getselectedvalues (); <br/> favoriate. settext (""); <br/> for (Object book: Books) <br/>{< br/> favoriate. append (book. tostring () + "/N"); <br/>}< br/> }); </P> <p> vector <string> bookcollection = new vector <string> (); <br/> bookcollection. add ("spring2.0 "); <br/> bookcollection. add ("Lightweight J2EE Enterprise Application Practice"); <br/> bookcollection. add ("J2EE-based Ajax Collection"); <br/> bookcollection. add ("struts2 authoritative guide"); <br/> bookcollection. add ("ror best practices for agile development"); <br/> // use a vector object to create a jcombobox object <br/> bookselector = new jcombobox (bookcollection ); <br/> // Add an event listener to jcombobox <br/> bookselector. additemlistener (New itemlistener () <br/>{< br/> Public void itemstatechanged (itemevent E) <br/>{< br/> // obtain the items selected in jcombobox <br/> Object book = bookselector. getselecteditem (); <br/> favoriate. settext (book. tostring (); <br/>}< br/>}); </P> <p> // You can edit the settings directly <br/> bookselector. seteditable (true); <br/> // set the visible height of the drop-down list box to display four list items at the same time <br/> bookselector. setmaximumrowcount (4); </P> <p> jpanel P = new jpanel (); <br/> P. add (bookselector); <br/> box = new box (boxlayout. x_axis); <br/> box. add (ListBox); <br/> box. add (p); <br/> mainwin. add (box); <br/> jpanel favoriatepanel = new jpanel (); <br/> favoriatepanel. setlayout (New borderlayout (); <br/> favoriatepanel. add (New jscrollpane (favoriate); <br/> favoriatepanel. add (New jlabel ("your favorite books:"), borderlayout. north); <br/> mainwin. add (favoriatepanel, borderlayout. south); </P> <p> mainwin. setdefaclocloseoperation (jframe. exit_on_close); <br/> mainwin. pack (); <br/> mainwin. setvisible (true); <br/>}</P> <p> private void addlayoutbutton (string label, final int orientation) <br/>{< br/> layoutpanel. setborder (New titledborder (New etchedborder (), "Confirm option layout"); <br/> jradiobutton button = new jradiobutton (Label ); <br/> // Add the radio button to the layoutpanel <br/> layoutpanel. add (button); <br/> // The first button is selected by default <br/> If (layoutgroup. getbuttoncount () = 0) <br/> button. setselected (true); <br/> layoutgroup. add (button); <br/> button. addactionlistener (New actionlistener () <br/>{< br/> Public void actionreceivmed (actionevent event) <br/>{< br/> // change the layout direction of the list items in the list box <br/> Booklist. setlayoutorientation (orientation); <br/>}< br/>}); <br/>}< br/> private void addselectmodelbutton (string label, final int selectmodel) <br/>{< br/> selectmodepanel. setborder (New titledborder (New etchedborder (), "Confirm mode"); <br/> jradiobutton button = new jradiobutton (Label ); <br/> // Add the single-choice button to the selectmodepanel <br/> selectmodepanel. add (button); <br/> // The first button is selected by default <br/> If (selectmodegroup. getbuttoncount () = 0) <br/> button. setselected (true); <br/> selectmodegroup. add (button); <br/> button. addactionlistener (New actionlistener () <br/>{< br/> Public void actionreceivmed (actionevent event) <br/>{< br/> // change the selection mode in the list box <br/> Booklist. setselectionmode (selectmodel); <br/>}< br/>}); <br/>}</P> <p> Public static void main (string [] ARGs) <br/>{< br/> New testlist (). init (); <br/>}< br/>
The difference between JList and JComboBox is that JComboBox only supports single choice, while JList can be multiple choices.