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: Gudal Mazucage
Country: Finland
Language: English (Spanish)
Genre: Literature
Published (Last): 22 November 2006
Pages: 380
PDF File Size: 2.72 Mb
ePub File Size: 7.55 Mb
ISBN: 198-4-55797-634-3
Downloads: 1112
Price: Free* [*Free Regsitration Required]
Uploader: Meztiran

Another possible value is JList. But, where are the rest of the countries? If no error occurs, it means the compilation is successful.

Using JList : JList « Swing « Java Tutorial

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:. Let us now add the following listener code: Run the program using the following command. How to Use Menus.

Sets the color used to draw the background of the selected items, which cell renderers can use to fill the selected cells. Modifying the Data Model Featured Books Java Coding Guidelines: But, ideally, we would want the scrollbar to appear in this case.


The array is filled with strings that were passed in from another object. You can specify a different selection mode by calling the setSelectionMode method on the list.

This table shows the examples that use JList and where those examples are described. A container for pseud code. Let us now run the program with this selection mode and try to select multiple items: A list uses an object called a cell renderer to display each of its items.

You can find the source code for ListDemo in ListDemo. You can process these events by adding a list selection listener to the list with the addListSelectionListener method.

JList basic tutorial and examples

We implement the valueChanged method. Then, we tutkrial use this model to create a JList: Customizing a JList Look and Feel This component also allows the user to select one or more elements visually.

Methods are used to find an item. For a VERTICAL layout orientation, this sets the preferred number of rows to display without requiring scrolling; for other orientations, it affects the wrapping of cells.

How to Use Lists

Set or get the property that determines whether automatic drag handling is enabled. Another common use of setVisibleRowCount is to specify to the lists’s scroll pane how many rows the list prefers to display.

These methods can be used to find the range of visible items. In our example, the strings happen to be boys’ names. For illustration purposes, we simply print this list on the console which prints the jaba of values selected.


ListModel getModel Returns the data model that holds the list of items displayed by the JList component. Using Swing Components Section: Returns the distance to scroll to expose the next or previous row for vertical scrolling or column for horizontal scrolling. Let us now run the program: Object getSelectedValue Returns the value tutoriial the smallest selected cell index; the selected value when only a single item is selected in the list.

Let us now try and change the selection mode.

SWING JList Class

Here is the code from ListDialog. Developing A Selection Listener: A JList presents the user with a group of items, displayed in one or more columns, to choose from. Sets the color used to draw the foreground of the selected items, which cell renderers can use to render text and graphics. Developing a Selection Listener.

From Journeyman to Master Code Complete: The following tables list the commonly used JList constructors and methods.