Java graphical user interface list box, java graphical list box

Source: Internet
Author: User

Java graphical user interface list box, java graphical list box

The list box is generated by the Swing component JList, which occupies a fixed number of rows on the screen. To obtain the selected element in the list box, you only need to call getSelectedValuesList () to generate a string array with the selected element name. The JList component allows multiple selections. If you press Ctrl, you can select all the clicked elements. If you select one element, press Shift and click another element, all elements between the two elements are selected. To remove one element from the selected element, press Ctrl to click the element.

After the list box is initialized, add and modify content to the list box. Static and dynamic operations.

1. static operations

The static operation adds all elements to the JList at the same time. After the elements are added, they cannot be modified or deleted, that is, they cannot be used to operate the list box during program execution.

E. g.

package test;import javax.swing.*;import java.awt.*;import static net.mindview.util.SwingConsole.*;public class ListTest1 extends JFrame{    private String[] str = {"Monday","Tuesday","Wednesday","Thursday","Friday","Staturday","Sunday"};    private JList list;public ListTest1()    {
list = new JList(str);
setLayout(new FlowLayout()); add(list); } public static void main(String[] args) { run(new ListTest1(),200,100); }}

For the above example, you only need to add all elements to the JList initialization.

Execution result: the list box cannot be operated.

2. dynamic operations

By checking JList, we can find that JList is not responsible for dynamic operations on the list box. All the details of dynamic operations can be completed in "list model", that is, defalistlistmodel, you only need to add the list model to the JList.

DefaultListModel listmodel = new DefaultListModel ();

Listmodel. addElement (element1); // Add an element

Listmodel. clear (); // clear all elements

Listmodel. remove (int index); // clear the element at the specified position

E. g.

Package test; import java. awt. *; import java. awt. event. *; import static net. mindview. util. swingConsole. *; import javax. swing. *; import javax. swing. border. border; import javax. swing. event. listSelectionEvent; import javax. swing. event. listSelectionListener; public class ListTest extends JFrame {private String [] str = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staturday ", "Sunday"}; private JButton button1 = new JButton ("Add Item"), button2 = new JButton ("Clear Item"); private JTextArea text = new JTextArea (str. length, 20); private defalistlistmodel listmodel = new DefaultListModel (); private JList list = new JList (listmodel );
// Add the list model to JList. The list model performs dynamic operations. JList creates a list and many other tasks (such as multiple choices ). Private int count = 0; private boolean flag = false; public ListTest () {text. setEditable (false); // only used for display. You cannot edit for (int I = 0; I <4; I ++) {listmodel. addElement (str [count ++]);} button1.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (count <str. length) {listmodel. addElement (str [count ++]);} else {button1.setEnabled (flag); flag = true ;}}); button2.addActionLi Stener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (count <str. length) {count = 0; // The list restarts to add the listmodel element. clear (); // clear text from the list element. setText (null);} else {count = 0; listmodel. clear (); text. setText (null); button1.setEnabled (flag); // start button }}); list. addListSelectionListener (new ListSelectionListener () {@ SuppressWarnings ("deprecation") public void valueChanged (ListSelectionEvent E) {if (e. getValueIsAdjusting () return; // if the event is detected to be being changed, true is returned, and subsequent statements are not executed. If the change is completed, false is returned and subsequent statements are executed. For (Object item: list. getSelectedValuesList () {text. append (item + "\ n"); // converts a List Object to an Object} // The getSelectedValuesList () method is called to generate a string array, the content is the name of the selected element}); setLayout (new FlowLayout (); Border border = BorderFactory. createMatteBorder (1, 1, 2, 2, Color. RED); // Add the border list. setBorder (border); // set the border text. setBorder (border); add (button1); add (button2); add (new JScrollPane (text); add (list);} public static void main (String [] args) {run (new ListTest (), 250,375 );}}

Execution result:

In the preceding procedure, the getValueIsAdjusting () method of the ListSelectionEvent event supported by JList and the getSelectedValuesList () method of JList are used during JList processing. Pay attention to the usage of the two methods.

(1) Boolean javax. swing. event. ListSelectionEvent. getValueIsAdjusting ()

Returns whether the event is one of multiple different events that are still being modified. If the event is one of multiple different events that are still being modified, true is returned.

For example, if you select to be updated to respond to a user's drag event, this attribute is set to true at the beginning of the drag and false at the end of the drag. During dragging, the listener receives events whose valueIsAdjusting attribute is set to true. At the end of the drag, when the change ends, the listener receives an event whose value is set to false.

If the registration program of the JList object is removed from the update detection statement:

if(e.getValueIsAdjusting())    return;

Output:

It can be seen that there is no update check. After the elements in the list box are selected, there are repeated outputs.

 

(2) List javax. swing. JList. getSelectedValuesList ()

The JList object calls the getSelectedValuesList () method to generate a string array with the selected element name.

3. JList scroll bar

JList does not directly support scrolling. We only need to wrap JList into JScrollPane, which will automatically help process all details.

Summary: to add elements to JList, you can perform static operations to add all elements during JList initialization, or use defalistlistmodel to process dynamic operations on all list modification details.

Note: Update detection may be used during JList element selection to ensure program stability.

Related Article

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.