Is there a way to get all elements in a JList?

35,513

Solution 1

Yes of course. You getElementAt() with a loop.

Example:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;

public class Jlist {
    JFrame frame;
    JList<String> list;
    JButton button;

    public Jlist(){
        init();
    }

    public void init(){
        frame = new JFrame("Sample");
        frame.setSize(300, 300);
        frame.setLayout(new  BorderLayout(30, 30));

        DefaultListModel<String> model = new DefaultListModel<>();
        model.addElement("A");
        model.addElement("B");
        model.addElement("C");
        model.addElement("D");
        model.addElement("E");

        list = new JList<>();

        list.setModel(model);
        JScrollPane pane = new JScrollPane(list);
        pane.setViewportView(list);

        button = new JButton("Get");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                buttonActionPerformed();
            }
        });


        frame.add(pane, BorderLayout.NORTH);
        frame.add(button, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    public void buttonActionPerformed(){
        for(int i = 0; i< list.getModel().getSize();i++){
            System.out.println(list.getModel().getElementAt(i));
        }
    }
    public static void main(String[] args) {
        new Jlist();
    }
}

Solution 2

You can use getModel method to get ListModel. And then use getElementAt and getSize method to build array or list or whatever you want.

Solution 3

try this...

 String[] myArray = new String[50];
    for (int i = 0; i < jList1.getModel().getSize(); i++) {
             myArray[i] = String.valueOf(jList1.getModel().getElementAt(i));
        }

Solution 4

I haven't seen anyone use the most concise way to do it. Assuming you're using Java 8+:

IntStream.range(0,listModel.size()).mapToObj(listModel::get).collect(Collectors.toList());

I personally prefer it to any of the loop methods.

Share:
35,513
Matthew Pigram
Author by

Matthew Pigram

I am a programmer currently employed at Village Roadshow Limited in Melbourne I am fluent in Java SE, C/C++/C#, ASP.NET and also have some knowledge of perl, sh, html, css, xml, mysql, mssql and python. I am also a certified Sitecore Developer.

Updated on July 18, 2022

Comments

  • Matthew Pigram
    Matthew Pigram almost 2 years

    I was wondering if there is a way to retrieve a list of all the elements that have been added to a JList. For example I would like JList to return an array or list of Strings or JLabels from a custom cell renderer.

  • PhoneixS
    PhoneixS about 9 years
    Also note that if you are using a DefaultListModel you can use the method toArray() to get an array of all the elements.