Tuesday, August 8, 2017

Java swing tutorial: Learning JList with code example

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 more  complex 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

Task: Implement JList which displays list of electronic trading in sorted ascending order.

Let's see an example of displaying JList in 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:

public class JListCodeDemo {

      public static void main(String args[]){
      JFrame listFrame = new JFrame();
      JPanel contentPane = (JPanel) listFrame.getContentPane();
      JList list = new JList(new AbstractListModel() {

      String[] companies = {"Stock trading", "Futures trading", "Options trading"};
      public int getSize() {
      return companies.length;

      public Object getElementAt(int index) {
      return companies[index];


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:

class ElectronicTradingListModel extends 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");


      public int getSize() {
      return electronic_trading_list.size();

      public Object getElementAt(int index) {
      return electronic_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

Further Reading
Java Fundamentals, Part 1 and 2
Mastering Java Swing - Part 1
Java Swing Development Using NetBeans

Here are some of my favorite java tutorial you may find interesting


Jirka Pinkas said...

I like task based tutorials, they are IMHO much easier to grasp.

I wonder why you didn't use DefaultListModel instead of AbstractListModel.

Anonymous said...

JList example is superb and task based tutorial is also a great idea of learning swing api.

Anonymous said...

are you guys still using Swing, isn't it good time to move to JavaFX?

Post a Comment