iliketurtles
iliketurtles

Reputation: 13

How to get all values from Jlist?

I have to find the longest word for each value from Jlist, not only a selected one. How do I get all values, without selecting anything?

I have the list defined :

list = new JList<String>(model);


btnNewButton_1.addActionListener(new ActionListener() { // Run
            public void actionPerformed(ActionEvent e) {
                String intxt = "", extxt = "";
                intxt = list.getSelectedValue();
                if (intxt == null) {
                    JOptionPane.showMessageDialog(null, "No chosen value", "Error",
                            JOptionPane.INFORMATION_MESSAGE);
                } else {
                    ...

Upvotes: 1

Views: 88

Answers (1)

George Z.
George Z.

Reputation: 6808

You can use a for loop in its ListModel and call getElementAt(int index) method:

JList<String> list = new JList<>();
for (int i = 0; i < list.getModel().getSize(); i++) {
    String listElement = list.getModel().getElementAt(i);

}

Upvotes: 2

Related Questions