Use JList with MVC Pattern

The Java Swing JList is a graphical component that displays a list of items, allowing the user to select one or more items from the list. In the Model-View-Controller (MVC) pattern, the JList is part of the View layer.

When a user interacts with a JList, several events are triggered. For example, if the user selects an item from the list, a selection event is fired. In the MVC pattern, these events are handled by the Controller layer.

To handle JList events in the MVC pattern, we can follow the following steps:

  1. Define the JList in the View layer, and set its selection mode, model, and renderer as needed.
  2. Create a custom ListSelectionListener in the Controller layer that implements the ListSelectionListener interface.
  3. Register the ListSelectionListener with the JList using the addListSelectionListener() method.
  4. Implement the valueChanged() method of the ListSelectionListener interface to handle the selection events. In this method, we can get the selected items from the JList and pass them to the Model layer for further processing.
  5. Implement any other necessary event handlers for the JList, such as mouseClicked() or mousePressed() for handling mouse events.

By separating the View and Controller layers, the MVC pattern makes it easier to modify and maintain our code. For example, we can change the appearance of the JList in the View layer without affecting the event handling in the Controller layer. Similarly, we can modify the event handling without changing the appearance of the JList.

Let’s see an example of JList using MVC pattern.

Model.java

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

public class Model {
	private List<String> items;

	public Model() {
		items = new ArrayList<>();
		items.add("Item 1");
		items.add("Item 2");
		items.add("Item 3");
	}

	public List<String> getItems() {
		return items;
	}

	public void addItem(String item) {
		items.add(item);
	}

	public void removeItem(int index) {
		items.remove(index);
	}
}

View.java

import java.awt.BorderLayout;
import java.awt.Container;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class View {
	private JFrame frame;
	private DefaultListModel<String> listModel;
	private JList<String> list;

	public View() {
		frame = new JFrame("JList MVC Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 200);

		listModel = new DefaultListModel<>();
		list = new JList<>(listModel);

		JPanel panel = new JPanel(new BorderLayout());
		panel.add(list, BorderLayout.CENTER);

		Container contentPane = frame.getContentPane();
		contentPane.add(panel);

		frame.setVisible(true);
	}

	public void updateList(List<String> items) {
		listModel.clear();
		for (String item : items) {
			listModel.addElement(item);
		}
	}

	public JList<String> getList() {
		return list;
	}
}

Controller.java

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Controller {
	private Model model;
	private View view;

	public Controller(Model model, View view) {
		this.model = model;
		this.view = view;

		view.getList().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (!e.getValueIsAdjusting()) {
					JList<String> source = (JList<String>) e.getSource();
					int index = source.getSelectedIndex();
					String item = source.getSelectedValue();
					JOptionPane.showMessageDialog(view.getList(), "Selected item: " + item);
				}
			}
		});

		view.updateList(model.getItems());
	}
}

JListMVCDemo.java


public class JListMVCDemo {
	public static void main(String[] args) {
        Model model = new Model();
        View view = new View();
        Controller controller = new Controller(model, view);
    }
}

When we run the JListMVCDemo.java it displays a frame containing JList with 3 items. See the below output:

Items displays from the JList in Jframe java swing

When a user selects an item from the list let’s say, Item 2, it displays the popup showing the output as a selected item. See below:

User selects item from the list mvc java swing
Sharing Is Caring: