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: Tygoshakar Kelkree
Country: Mauritania
Language: English (Spanish)
Genre: Health and Food
Published (Last): 23 July 2015
Pages: 303
PDF File Size: 20.85 Mb
ePub File Size: 3.34 Mb
ISBN: 153-2-20490-849-8
Downloads: 17209
Price: Free* [*Free Regsitration Required]
Uploader: Nibei

Examples and practices described in this page don’t take advantage of improvements introduced in later releases.

A JList presents the user with a group of items, displayed in one or more columns, to choose from. Lists can have many items, so they are often put in scroll panes. In addition to lists, the following Swing components present multiple selectable items to the user: To display hierarchical data, use a tree.

The following figures shows two applications that use lists. This section uses these examples as a basis for the discussions that follow. Here is the code from ListDialog.

The code passes an array to the list’s constructor. The array is filled with strings that were passed in from another object.

In our example, the strings happen futorial be boys’ names. Other JList constructors let you initialize a list from a Vector or from an object that adheres to the ListModel interface.

Java JList Class Example

If you initialize a list with an array or vector, the constructor implicitly creates a default list model. To create a list whose items can be changed individually, set the list’s model to an instance of a mutable list model class, such as an instance of DefaultListModel. You can set a list’s model when you create the list or by calling the setModel method.

The call to setSelectionMode specifies how many items the user can select, and whether they must be contiguous; the next section tells you more about selection modes. The call to setLayoutOrientation lets the list display its data in multiple columns. Another possible value is JList.

The following figures show these two wrapping possibilities, together with the default, JList. In combination with the call to setLayoutOrientationinvoking setVisibleRowCount -1 makes the list display the maximum number of items possible in the available space onscreen.

Another common use of setVisibleRowCount is to specify to the lists’s scroll pane how many rows the list prefers to display. A list uses an instance of ListSelectionModel to manage its selection. By default, a list selection model allows any combination of items to be selected at a time. You can specify a different selection mode by calling the setSelectionMode method on the list. The following table describes the three list selection modes:. No matter which selection mode your list uses, the list fires list selection events whenever the selection changes.

You can process these events by adding a list selection listener to the list with the addListSelectionListener method. A list selection listener must implement one method: Here is the valueChanged method for the listener in ListDemo:. Many list selection events can be generated from a single user action such as a mouse click.


The getValueIsAdjusting method returns true if the user is still manipulating the selection. This particular program is interested only in the final result of the user’s action, so the valueChanged method does something only if getValueIsAdjusting returns false.

Because the list is in single-selection mode, this code can use getSelectedIndex to get the index of the just-selected item. JList provides other methods for setting or getting the selection when the selection mode allows more than one item to be selected. If you want, you can listen for events on the list’s list selection model rather than on the list itself. ListSelectionDemo is an example that shows how to listen for list selection events on the list selection model and lets you change the selection mode of a list dynamically.

The ListDemo example that we showed previously features a list whose contents can change. You can find the source code for ListDemo in ListDemo. Here is the ListDemo code that creates a mutable list model object, puts the initial items in it, and uses the list model to create a list:.

SWING JList Class

This particular program uses an instance of DefaultListModela class provided by Swing. In spite of the class name, a list does not have a DefaultListModel unless your program explicitly makes it so.

If DefaultListModel does not suit your needs, you can write a custom list model, which must adhere to the ListModel interface. The following code snippet shows the actionPerformed method for the action listener registered on the Fire button. The bold line of code removes the selected item in the list. The remaining lines in the method disable the fire button if the list is now empty, and make another selection if it is not. Here is the actionPerformed method for the action listener shared by the Hire button and the text field:.

This code uses the list model’s insertElementAt method to insert the new name after the current selection or, if no selection exists, at the beginning of the list. If you just wish to add to the end of the list, you can use DefaultListModel ‘s addElement method instead.

Whenever items are added to, removed from, or modified in a list, the list model fires list data events. That section contains an example that is similar to ListDemobut adds buttons that move items up or down in the list.

A list uses an object called a cell renderer to display each of its items.

The default cell renderer knows how to display strings and icons and it displays Object s by invoking toString. If you want to change the way the default renderer display icons or strings, or if you want behavior different than what is provided by toStringyou can implement a custom cell renderer. Take these steps to provide a javq cell renderer for a list:.

See the example described in Providing a Custom Renderer. The following tables tutrial the commonly used JList constructors and methods. Other methods you are most likely to invoke on a JList object are those such as setPreferredSize that its superclasses provide.

Much of the operation of a list is managed by other objects. The jlisr in the list are managed by a list model object, the selection is managed by a list selection model object, and most programs put a list in a scroll klist to handle scrolling. For the most part, you do not need to worry about the models because JList creates them as necessary and you interact with them implicitly with JList ‘s convenience methods.


This table shows the examples that use JList and where those examples are described. List View tutorial to learn how to create tutorkal in JavaFX. Using Swing Components Section: How to Use Various Components. Alternatively, to compile and run the example yourself, consult the example index. Click the Launch button to run ListDialogRunner.

To bring up the ListDialog, click the Pick a new name The resulting dialog is a ListDialog instance that has been customized to have the title Name Chooser. In ListDemo, try adding hiring and removing firing a few items. How to Use Layered Panes Next page: How to Use Menus. Only one item can be jwva at a time. When the user selects an item, any previously selected item is deselected first.

Multiple, contiguous items can be selected. When the user begins a new selection range, any previously selected items are deselected first. Any combination of items can be jvaa. The user jxva explicitly deselect items. Create a list with the initial list items specified.

The second and third constructors implicitly create an immutable ListModel ; you should not subsequently modify the passed-in array or Vector. Set or get the visibleRowCount property.

See the setLayoutOrientation int for more information. Set or get the way list jaba are laid out. Set the current selection as indicated. Use setSelectionMode to set what ranges of selections are acceptable. The boolean argument specifies whether the list should attempt to scroll itself so that the selected item is visible.

Set or get the selection mode. 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. The third argument, which specifies the search direction, can be either Position. For example, if you have a 6-item list, getNextMatch “Matisse”, 5, javax. Forward searches for the string “Matisse” in the item at index 5, then if necessary at index 0, index 1, and so on.

Set or get the property that determines whether automatic drag handling is enabled. See Drag and Drop and Data Transfer for more details. Contains a list and a table that share the same selection model. You can dynamically choose the selection mode.

Shows how to provide a custom renderer for a combo box. Because lists and combo boxes use the same type of renderer, you can use what you learn there an apply it to lists. In fact, a list and a combo box can share a renderer.