Skip to main content

Swing Tutorials: Custom JList 2

The previous tutorial on Custom JList here covers the creation of a simple JList displaying a list of countries. This tutorial introduces the implementation of a custom cell renderer to achieve pretty cool stuff with the JList. Here, the list of countries appear with not just their names, but also their flags. First, we begin by modifying the Country model. We add a variable to hold the name of the country's flag. The flag is added in the same package as the view class. package com. coolcodingtutorials . model ; /** * * @author jaletechs */ public class Country { private String name; private int id; public Country ( int id, String name) { this . name = name; this . id = id; } public String getName () { return name; } public void setName (String name) { this . name = name; } public int getId () { return id; } public void setId ( int id) { this . id = i...

Swing Tutorials: Custom JList

The JList is a cool swing component that is used to display a list. Unlike a drop down or a JComboBox in swing, a JList displays all of its items at once, allowing the user to scroll in the event that the other items are not visible on the screen. For good UI/UX sometimes, a JList is sometimes the perfect widget for displaying a list of items that could trigger some actions on the UI. So let's get down to it.

First we create a custom object. We wish our JList to display a list of countries, so we create a country object as seen below:

package com.coolcodingtutorials.model;
/**
*
* @author jaletechs
*/
public class Country {
private String name;
private int id;
public Country(int id, String name) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + this.id;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Country other = (Country) obj;
if (this.id != other.id) {
return false;
}
return true;
}
@Override
public String toString(){
return name;
}
}


Note: the toString() method in the Country model is overridden to display the name of the country

Next, we create a factory class to generate a dummy list of countries. In a real application, this data would probably come from a database.

package com.coolcodingtutorials.model;

import java.util.List;
import java.util.ArrayList;

/**
 *
 * @author jaletechs
 */
public class CountryFactory {
    public static List<Country> getCountries(){
        ArrayList<Country> countries = new ArrayList<>();
        
        countries.add(new Country(1, "Nigeria"));
        countries.add(new Country(1, "USA"));
        countries.add(new Country(3, "Belgium"));
        
        return countries;
    }
}

Next, we create a view class that creates a small window and displays the list of countries generated from our country factory on a JList

package com.coolcodingtutorials.view;

import com.coolcodingtutorials.model.Country;
import com.coolcodingtutorials.model.CountryFactory;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

/**
 *
 * @author jaletechs
 */
public class MainWindow {
    public static void main(String[] args) {
        MainWindow window = new MainWindow();
        window.go();
    }
    
    private void go(){
        //create a frame
        JFrame frame = new JFrame("Custom JList");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //create a JList
        JList<Country> jList = new JList(CountryFactory.getCountries().toArray());
        
        //add JList to a Scroll pane
        JScrollPane scroller = new JScrollPane(jList);
        
        frame.getContentPane().add(scroller);
        
        
        frame.setSize(300,300);
        frame.setVisible(true);
    }
}


When the MainWindow.java class is run, the expected output is a window / frame
with a JList inside and a list of the countries in our country factory. 
Feel free to play around with the code, and I'll see you in the next tutorial

Output:





Comments