简体   繁体   中英

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM