Apps/Gaming

How to Use Lists in Your Java GUI Application

Java allows developers to create lists for their graphical applications using the JList class, which is a subclass of the JComponent class. It enables programmers to create a list of items from which a user can choose an option. Lists are normally put in scroll panes since they can contain many items.

In this Java programming tutorial, we will discuss how coders can use JList in graphical user interface (GUI) applications.

Reading: How to Create a Java ArrayList Class

How to Create a Model in Java

A model allows your program to track an object’s state. JList has three models that you can use:

  • DefaultListModel: This is the simplest model to use and it handles everything for you.
  • AbstractListModel: Allows you to invoke “fire” methods and manage your data.
  • ListModel: Gives you complete control over everything.

JList models allow your program to keep track of items added to – or selected on – the list. The examples in this tutorial will be using the DefaultListModel. Here is an example of its syntax in Java:

DefaultListModel fruits = new DefaultListModel<>(); // sample usage

How to Initialize and Select List Items in a Java Model

You can add items to your model using the addElement() method. If you wish to remove an item from the list, then you simply use the remove() method and provide the index of the element as the parameter, as shown in the following code example:

fruits.addElement(“banana”); fruits.remove(2);

You can then initialize your list using the strings passed from another object (for example, fruits in the example below):

JList list = new JList<>(fruits);

In the above Java code, we initialized our Jlist model with the items in the fruits list.

How to Allow User Selections on Java Lists

When dealing with a list in Java, it is necessary for you to define how the user makes selections (provided you want to allow this functionality, mind). The setSelectionMode() method allows you to do this. It defines the number of selections a user can make and whether or not they are contiguous or in sequence. There are three selection modes for Java lists:

  • SINGLE_SELECTION: A user can only select one list item at a time
  • SINGLE_INTERVAL_SELECTION: A user can select multiple contiguous or sequential items
  • MULTIPLE_INTERVAL_SELECTION: A user can make any combination of selections. This is the default selection mode

These three selection modes are constants defined in the ListSelectionModel that manages them. Here is an example of how to define the selection mode of a list in Java:

list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

Reading: Using Nested Classes in Java

How to Wrap Data in Columns Using Java

A list may have multiple items that have to be presented to the user across more than one column. The setLayoutOrientation() method enables you to define the way in which data is presented or wrapped over multiple columns. There are three different ways to wrap data in Java, detailed below:

JList.HORIZONTAL_WRAP: Items are presented from left to right before wrapping to a new row
JList.VERTICAL_WRAP: Items are presented from top to bottom before wrapping to a new column
JList.VERTICAL This is the default. Items are simply presented in a single row

Here is a code example showing how to use the setLayoutOrientation() method in Java:

// code example showing how to use setLayoutOrientation() list.setLayoutOrientation(JList.HORIZONTAL_WRAP);

See the full code example below, which demonstrates the use of Java lists using JList. The code shows all of the concepts we have covered so far. It is also important for you to remember that JList has to be part of a containment hierarchy that has a top-level container as its root. The JFrame top-level container is used here:

import javax.swing.*; import javax.swing.event.*; class JlistExample{ public static void main(String args[]){ JFrame frame = new JFrame(); frame.setTitle(“Fruits”); // #1 create a model DefaultListModel fruits = new DefaultListModel<>(); fruits.addElement(“banana”); fruits.addElement(“mango”); fruits.addElement(“orange”); fruits.addElement(“apple”); fruits.addElement(“berry”); fruits.addElement(“lemon”); fruits.addElement(“carrot”); fruits.addElement(“grapes”); JList list = new JList<>(fruits); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.setLayoutOrientation(JList.HORIZONTAL_WRAP); frame.add(list); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(225, 125); frame.setVisible(true); } }

Summing Up Java Lists in GUI Applications

It is worth noting that developers can also initialize your list items using an array or vector. However, the list would then be immutable, meaning that you cannot add or remove any items to or from it. Therefore, to give you the flexibility of modifying your list, it is better to use a list model, as demonstrated in this tutorial.

read more Java programming tutorials and Java developer tool reviews.

Related posts

Java Primitive Data Types

TechLifely

How to implement linked lists in Go

TechLifely

Top collaboration tools for software developers

TechLifely

Leave a Comment