Java Graphical user interface list box _java

Source: Internet
Author: User

The list box is generated by the swing component jlist, which always occupies a fixed number of rows on the screen. To get the selected element in the list box, simply call Getselectedvalueslist (), which produces an array of strings with the selected element name. JList components allow multiple selections; If you hold down the CTRL key, you can select all the clicked elements, and if you select an element, hold down the SHIFT key and click another element, all elements between the two elements are selected, and Ctrl-click the element to remove one from the selected element.

After you initialize the list box, you add and modify the contents to the list box. It is divided into static operation and dynamic operation.

1. Static operation

Static operation is to add all the elements to the jlist, can not be modified after adding, can not be deleted, that is, in the process of execution can not manipulate the list box.

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);
}

As in the previous example, all elements are added as soon as the jlist is initialized.

Execution Result: The list box cannot be manipulated.

2. Dynamic operation

By looking at the JList method, you can see that jlist is not responsible for the dynamic operation of the list box, and that the details of all the dynamic operations can be done in the list model, Defaultlistmodel, simply by adding the list model to JList.

Defaultlistmodel Listmodel = new Defaultlistmodel ();
Listmodel.addelement (element1);//add Element
listmodel.clear ();//Clear all elements
listmodel.remove (int index); Clears the element from the specified location

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 Defaultlistmodel Listmodel = new Defaultlistmodel ();
Private JList list = new JList (Listmodel);
Add a list model to JList, the list model is responsible for dynamic operations, JList is responsible for creating lists, and many other tasks, such as multiple selections.
private int count = 0;
Private Boolean flag = false; Public Listtest () {text.seteditable (false);//Only for display, cannot edit for (int i = 0;i<4;i++) {listmodel.addelement (str[count++]
);
} button1.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {if (count<str.length) {LISTMODEL.ADDElement (str[count++]);
}else {button1.setenabled (flag); flag = True;}}
});
Button2.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {if (count<str.length) {
Count = 0;//list to restart adding element listmodel.clear ();//list element clear Text.settext (NULL);
}else {count = 0; Listmodel.clear (); Text.settext (null); button1.setenabled (flag);//Startup Button}}}); List.addlistselectionlistener (New Listselectionlistener () {@SuppressWarnings ("deprecation") public void ValueChanged (listselectionevent e) {if (e.getvalueisadjusting ()) return;//If the event is detected to be changed, returns TRUE, the following statement does not execute, and when the change is completed, the Returns FALSE, executing the following statement. For (object Item:list.getSelectedValuesList ()) {text.append (item + \ n);//list object converted to object}//
The list calls the Getselectedvalueslist () method, which produces an array of strings, with the contents of the selected element name}};
SetLayout (New FlowLayout ());
Border Border = Borderfactory.creatematteborder (1, 1, 2, 2, color.red);//Add Border List.setborder (Border);//Set 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 results:

In the above program, the Getvalueisadjusting () method of the event listselectionevent supported by JList and the JList () method of jlist are used in the process of processing the You need to be aware of the use of these two methods.

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

Returns whether this event is one of several different events that are still being changed, and returns True if this event is one of several different events that are still being changed.

For example, this property is set to True when the drag begins and is set to false at the end of the drag, for events where the selection is updated to respond to a drag of a user. During drag, the listener receives an event with the Valueisadjusting property set to True. At the end of the drag, when the change terminates, the listener receives an event with a value set to false.

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

if (e.getvalueisadjusting ()) return
;

The output is:

Visible, there is no update detection, the list box element is selected, there is duplicate output.

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

The JList object invokes the Getselectedvalueslist () method, which produces an array of strings with the selected element name.

3. JList scroll bar

JList does not provide direct support for scrolling, we simply wrap jlist into JScrollPane, which automatically helps with all the details.

Summary: JList want to add elements, you can perform static operations that add all elements to the jlist initialization, or you can use the list model Defaultlistmodel to process all of the list modification details in a dynamic operation.

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

The above is a small series to introduce the Java Graphical user interface list box, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.