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 ?

vdolez
  • 977
  • 1
  • 14
  • 33
  • 1
    Possible duplicate of [How can I convert a Java HashSet to a primitive int array?](http://stackoverflow.com/questions/2451184/how-can-i-convert-a-java-hashsetinteger-to-a-primitive-int-array) – Tom Jul 26 '16 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. – vdolez Jul 26 '16 at 11:42
  • 1
    Swing, since1.2, predates generics, since 1.5. Why not overload `setSelectedIndices()`. – trashgod Jul 26 '16 at 11:45
  • 1
    @trashgod : Create a class that inherits JList for this purpose ? Yeah, that could be a solution I'll use. – vdolez Jul 26 '16 at 11:47

2 Answers2

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);
        }
    }
}
TMichelsen
  • 327
  • 1
  • 7
  • 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 :) – vdolez Jul 26 '16 at 12:38
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);
        }
    }
trashgod
  • 203,806
  • 29
  • 246
  • 1,045