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: Tabar Goltill
Country: Uruguay
Language: English (Spanish)
Genre: Business
Published (Last): 21 June 2016
Pages: 101
PDF File Size: 19.50 Mb
ePub File Size: 15.94 Mb
ISBN: 630-5-67082-862-7
Downloads: 90122
Price: Free* [*Free Regsitration Required]
Uploader: Tojakree

SWING – JList Class

Developing a Selection Listener. To make multiple list selections, you can use the anchor and lead indices of the selection items.

Let us now run the program and select jjlist items. In fact, a list and a combo box can share a renderer. To make this happen, we have to add the list to a scrollpane instead of adding it directly: Any combination of items can be selected.

This section uses these examples as a basis for the discussions that follow. If it is not, we retrieve the selected items.

Using JList : JList « Swing « Java Tutorial

The resulting dialog is a ListDialog instance that has been customized to have the title Name Chooser. JList is a Swing component with which we can display a list of elements.


You can process these events by adding a list selection listener to the list with the addListSelectionListener method. We create an instance of the DefaultListModel class tytorial declaring it as accepting only String values using the parameterized syntax.

Other JList constructors let you initialize a list from a Vector or from an object that adheres to the ListModel interface. Many list selection events can be generated from a single user action such as a mouse click. In our example, the strings happen to be boys’ names.

Point indexToLocation int index Returns the origin of the specified item in the list’s coordinate system.

How to Use Lists

Multiple, contiguous items can be selected. Given the starting index, search through the list for an item that starts with the specified string and return that index or -1 if the string is not found. Get index of last visible item Returns the smallest selected cell index; the selection when only a single item is selected in the list. Color getSelectionForeground Returns the color used to draw the foreground of selected items.

Java JList class example | Wideskills

That section contains an example that is similar to ListDemobut adds buttons that move items up or down in the list. The following figures shows two applications that use lists.


If DefaultListModel does not suit your needs, you can write a custom list model, which must adhere to the ListModel interface. Traps, Pitfalls, and Corner Cases. The code passes an array to the list’s constructor. Bench mark for JList DropLocation getDropLocation Returns the location that this component should visually indicate as the drop location during a DnD operation over the component, or null if no location is to currently be shown.

If you initialize a list with an array or vector, the constructor implicitly creates a default list model. Using a Custom Data Model Event handling in JList is very similar to that of other Swing components. Sets the visibleRowCount property, which has different meanings depending on the layout orientation: Returns an array of all the selected values, in an increasing order based on their indices in the list.

Set or get the selection mode. Returns the distance to scroll to expose the next or previous row for vertical scrolling or column for horizontal scrolling. It is important to note that the JList declaration and usage should also be parameterized.