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.

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) {

	public void removeItem(int index) {

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.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();


	public void updateList(List<String> items) {
		for (String item : items) {

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

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() {
			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);


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