There are three ways to create a list model: DefaultListModel — everything is pretty much taken care of for you. The examples in this page use DefaultListModel. AbstractListModel — you manage the data and invoke the "fire" methods. ListModel — you manage everything. Here is the code from ListDialog. The array is filled with strings that were passed in from another object.

Author:Samurn Taular
Country:Timor Leste
Language:English (Spanish)
Published (Last):17 July 2013
PDF File Size:1.53 Mb
ePub File Size:13.55 Mb
Price:Free* [*Free Regsitration Required]

Let us now run the program: 3. Items can be selected contiguously by pressing down the shift key and selecting elements with the mouse.

This mode specifies that multiple items can be selected and they may or may not be contiguous. Let us now run the program and select multiple items. We will hold down the shift key and click with mouse to select contiguous items.

For non-contiguous items, we need to press down the ctrl key and select with the mouse. We get the following output: Output with Multiple Items Selected Let us now try and change the selection mode. This can be done as follows: countryList. Let us now run the program with this selection mode and try to select multiple items: Single Selection Mode When we run the program with this change and try to select multiple items by pressing down either the shift or ctrl key , we find out that we are unable to select multiple items.

Event Handlers: Let us now try and develop event handlers for JList. Event handling in JList is very similar to that of other Swing components. We register an event listener and are notified when the event happens.

Knowing the currently selected item s will be one of the most useful events to know. Note that, the user might have selected one or more item s in the list. Also, the user may deselect a selected item. If the user had earlier selected only one item and now deselected the same, there would be no selection left in the list.

The selection listener for the list fires the event even in the case of user deselecting an item. This might be quite useful in certain scenarios. Let us now add the following listener code: countryList. We use an anonymous inner class to implement the event listener interface.

We implement the valueChanged method. For illustration purposes, we simply print this list on the console which prints the list of values selected. Now, run the program and select a few items single, multiple, contiguous, non-contiguous and check the output on the console. Now, deselect all items in the list and check the output on the console.

It will print an empty list. We use extra if condition in the code which checks if the list is still adjusting the status. If it is not, we retrieve the selected items. Here is the full source code of our GUI: package net. List; import javax. DefaultListModel; import javax. JFrame; import javax. JList; import javax. JScrollPane; import javax. SwingUtilities; import javax. ListSelectionEvent; import javax.


How to Use Lists



JList basic tutorial and examples



JList « Swing « Java Tutorial


Related Articles