2

JList declares a method : public void setSelectedIndices(int[] indices)

Which according to Oracle's documentation changes :

the selection to be the set of indices specified by the given array

I know that a Set can not take primitive int as a generic type.

How to retrieve the indices I want to select? For now, I use a Set<Integer>, loop through the rows and when a row is selected a add it to the Set<Integer>.

But then, I must convert the Set<Integer>into a int[] and there is no built-in function to do that. I already know how to do the translation.

Does anyone who uses public void setSelectedIndices(int[] indices) has to develop his own converter or use ones of Guava or Apache ?

This seems to be a flaw in Swing. Is there any reason why this method can not take a Set or even a List ?

Or is there a way to create a set of primitive that I don't know ?

Wouldn't it be better for every developer if the method took a Set<Integer> as argument ? Why isn't the case ?

4
  • 1
    Possible duplicate of How can I convert a Java HashSet<Integer> to a primitive int array? Commented Jul 26, 2016 at 11:32
  • @Tom : this question asks specifically if there's a flaw in Swing. I already know how to convert a Collection to an Array. What I want to know is why this specific method take an array of primitive int when it only oblige its users to do a translation that is NOT in the java standard. Commented Jul 26, 2016 at 11:42
  • 1
    Swing, since1.2, predates generics, since 1.5. Why not overload setSelectedIndices(). Commented Jul 26, 2016 at 11:45
  • 1
    @trashgod : Create a class that inherits JList for this purpose ? Yeah, that could be a solution I'll use. Commented Jul 26, 2016 at 11:47

2 Answers 2

2

Swing, available since Java 1.2, predates generics, introduced in Java 1.5. In the interim, generics have been added to Swing where the benefit outweighs the risk of causing backwards incompatibility. For example, JList itself has been JList<E> since Java 1.7. Because setSelectedIndices() "is a convenience method that clears the selection and then uses addSelectionInterval() on the selection model to add the indices," you can overload the method as desired (range checking elided for clarity):

private static class MyList extends JList {

    public void setSelectedIndices(Set<Integer> indices) {
        ListSelectionModel model = getSelectionModel();
        model.clearSelection();
        for (Integer index : indices) {
            model.addSelectionInterval(index, index);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the SelectionModel as a workaround:

Set<Integer> indices = ...
...
myList.getSelectionModel().clearSelection()
for( Integer index : indices) {
    myList.getSelectionModel().addSelectionInterval(index, index);
}

Inspired from JList.setSelectedIndices():

public void setSelectedIndices(int[] indices) {
    ListSelectionModel sm = getSelectionModel();
    sm.clearSelection();
    int size = getModel().getSize();
    for (int i : indices) {
        if (i < size) {
            sm.addSelectionInterval(i, i);
        }
    }
}

1 Comment

While this is a viable way to do it, I would prefer a reusable solution. I don't want to code a for loop each time I need to set indices. Thanks for your answer anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.