Pages

In this java swing JList tutorial, we will learn important properties and function of JList while working on a task with examples. In my opinion best way to learn swing is to actually doing the task and so I designed this tutorial around the task. We will start with a simple example and proceed with morecomplex one step by step adding new functionality and in the course, we will learn JList both concept and API. In this first example, we need to implement a list which shows names of companies in sorted order. Before start coding let's see some fundamentals of JList:1) JList is a GUI element available in javax.swing package which allows selection of one more element. Internally JList is backed up by two models: ListModel for holding data and ListSelectionModel for handling selection. JList also allows custom rendering via ListCellRenderer which can be used to draw a specific cell in a customizing manner.

2) JList elements are uneditable, JList does not support any rendering.

3) There are two ways to set model of JList either using JList.setModel() or JList.setListData().

4) JList implements Scrollable interface and can be put on Scrollpane. JList also supports auto-scrolling. visibleRowCount property of JList is used to display a number of rows when a JList is placed on Scrollpane.you can change it by using method setVisibleRowCount().

5) JList cell's width and height is determined by width of widest element and height of highest element but you can also customize it using methods setFixedCellWidh() and setFixedCellHeight().

JList example in Java Swing

Showing JList in Swing is very easy what you need to do is: create a JList, set its model, put the JList in Panel and done. For creating model for JList you can extend AbstractListModel class which implements ListModel and you can store your data inside this class in either Array or Collection classes as shown in below example of JList:

Now to display elements in Sorted Order we need to sort the data which we are going to show in JList. For this purpose, we will create our own custom ListModel and store our data in List and then sort this list using Collections.sort() method which will sort Strings on their natural order. Here is the code:

classElectronicTradingListModelextends AbstractListModel{

private List electronic_trading_list = new ArrayList();

public ElectronicTradingListModel(){

electronic_trading_list.add("Equity Trading");

electronic_trading_list.add("Futures Trading");

electronic_trading_list.add("Options Trading");

electronic_trading_list.add("Currency trading");

Collections.sort(electronic_trading_list);

}

@Override

publicint getSize() {

returnelectronic_trading_list.size();

}

@Override

public Object getElementAt(int index) {

returnelectronic_trading_list.get(index);

}

}

Now just set this model as your list model by adding following line of code:

list.setModel(new ElectronicTradingListModel());

Let me know how you find this example of JList in Swing, how is my idea of having task-based tutorial. To be frank I really liked this thought because it provides you a focus which is quite important otherwise you will lose in this huge API. So focus here is to learn fundamentals of JList and Swing by doing actual real world task. In the next JList tutorial we will see how to implement custom rendering on JList by our next task: Display currency trading in yellow color and Stock trading on red color