JList - select multiple items
Asked Answered
A

4

18

I faced a problem with this setSelectedValue() method in JList when I wanted to select multiple values in a JList automatically, it still selected only one. Is there a way?

 String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" };
      final JList theList = new JList(items);
      theList.setSelectedValue("Item 1",true);
      theList.setSelectedValue("Item 2",true);

This code shows only Item 2 as selected.

Ashby answered 4/6, 2011 at 5:18 Comment(0)
H
13

Use JList.setSelectedIndices(int[]) after calling JList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION).

E.G.

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
class MultiSelectList {
    public static void main(String[] args) throws Exception {
        File f = new File("MultiSelectList.java");
        InputStream is = new FileInputStream(f);
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        final ArrayList<String> lines = new ArrayList<String>();
        String line = br.readLine();
        while (line!=null) {
            lines.add(line);
            line = br.readLine();
        }
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JList list = new JList(lines.toArray());
                list.setSelectionMode(
                    ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
                int[] select = {19, 20, 22};
                list.setSelectedIndices(select);
                JOptionPane.showMessageDialog(null, new JScrollPane(list));
            }
        });
    }
}

Screen Shot

Screen shot of code

Hardhack answered 4/6, 2011 at 8:38 Comment(1)
@ Nirav follows this Java Swing tutorial download.oracle.com/javase/tutorial/uiswing/components/… and tons of examples for that on java2s.com/Code/Java/Swing-JFC/List.htm +1Inwrap
C
4
list.getSelectionModel().setSelectionInterval(...);

or if the selection isn't consecutive then you need to use multiple

list.getSelectionModel().addSelectionInterval(...);
Carrol answered 4/6, 2011 at 6:3 Comment(1)
yes, It right but I need to select them on the base of values not on index.do have any idea for that?Ashby
T
4

As you are using the NetBeans GUI editor, you can customize the Post-Creation Code generated for your JList as shown below.

post-creation dialog

Thorlay answered 4/6, 2011 at 10:3 Comment(3)
@Nirav: As an exercise, compare @Andrew Thompson's example to the generated code.Thorlay
now problem is that every time first item is shown as selected. otherwise it works fine.Ashby
@Nirav: Correct, a call to setSelectedIndices(), such as @Andrew Thompson has shown, must also be added.Thorlay
C
0
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;

class MultiSelectList {
    public static void main(String[] args) throws Exception {
        File f = new File("MultiSelectList.java");
        InputStream is = new FileInputStream(f);
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        final ArrayList<String> lines = new ArrayList<String>();
        String line = br.readLine();
        while (line!=null) {
            lines.add(line);
            line = br.readLine();
        }
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JList list = new JList(lines.toArray());
                list.setSelectionMode(
                    ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
                int[] select = {19, 20, 22};
                list.setSelectedIndices(select);
                JOptionPane.showMessageDialog(null, new JScrollPane(list));
            }
        });
    }
}
Cimabue answered 28/6, 2011 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.