Site Loader

Using JList import yout; import ; import javax. ; import lPane; public class JListTest { public static. JList is part of Java Swing package. JList is a component that displays a set of Objects and allows the user to select one or more items. JList inherits. In this example we are going to demonstrate how to use Java Swing JList, JList is a Swing component with which we can display a list of.

Author: Nimi Tushicage
Country: Lithuania
Language: English (Spanish)
Genre: History
Published (Last): 18 October 2011
Pages: 433
PDF File Size: 13.53 Mb
ePub File Size: 6.96 Mb
ISBN: 198-4-73040-266-1
Downloads: 8855
Price: Free* [*Free Regsitration Required]
Uploader: Shakakinos

JList is a Swing component with which we can display a list of elements. This component also allows the user to select one or more elements visually. This article shows how to work with JList and proceeds to show some examples. Developing A Simple JList.

Developing a Selection Listener. The preceding piece of code is quite javw. We create an instance of the DefaultListModel class by declaring it as accepting only String values using the parameterized syntax. Then, we can use this model to create a JList: It is important to note that the JList declaration and usage should also be parameterized. Let us provide the full source code and run it: Resized Frame without Scrollbar.

But, where are the rest of the countries?

JList basic tutorial and examples

They have disappeared from the display. But, ideally, we would want the scrollbar to mlist in this case. To make this happen, we have to add the list to a scrollpane instead of adding it directly: Let us now run the program: The selection mode defines the way elements can be selected.

There are totally 3 selection modes available to javw set for JList: This mode specifies that only a single item can be selected at any point of time. This mode specifies that multiple items can be selected, but they have to be contiguous. Items can be selected contiguously by pressing down the shift key and selecting elements with the mouse. This mode is the default mode. This mode specifies that multiple items can be selected and they may or may not be contiguous.

  LOS ANARQUISTAS EXPROPIADORES PDF

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. Changing the Selection Mode: Let us now try and change the selection mode.

This can be done as follows: We make a call to the setSelectionMode method and make use of the constants declared in the ListSelectionModel to set our selection mode.

Let us now run the program with this selection mode and try to select multiple items: When we run the program with this change and try to select multiple items by jliet down either the shift or ctrl keywe find out that we are unable to select multiple items. Let us now try and develop event handlers for JList.

Java JList Class Example

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.

Let us write a selection listener for JList. 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.

  AMPROBE 30XR-A PDF

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: 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 ajva. Here is the full source code of our GUI: Notify me of follow-up comments.

Java JList class example | Wideskills

Featured Books Java Coding Guidelines: From Journeyman to Master Code Complete: JList basic tutorial and examples. Output with Scrollbar 3. Developing A Selection Listener: Add comment Notify me of follow-up comments. Refresh comments list RSS feed for comments to this post.

Head First Design Patterns. Java Concurrency in Practice. Traps, Pitfalls, and Corner Cases. A Handbook of Agile Software Craftsmanship.

Using JList : JList « Swing « Java Tutorial

Improving the Design of Existing Code. From Journeyman to Master. Cracking the Coding Interview: