So I have a JList in my program and it starts with the selected index 0. However, during the program I want to do myJList.setSelectedIndex(-1) or something like that so that nothing is selected ? Hope I was clear. Thanks
            Asked
            
        
        
            Active
            
        
            Viewed 2,822 times
        
    2 Answers
4
            JList has a method to clear the selection. See JList#clearSelection. Check JList#getSelectionModel and the ListSelectionModel API to discover what methods are available to alter the selection
Edit: since you indicated it does not work, I created a SSCCE showing the clearSelection works as expected
public class Test {
  public static void main( String[] args ) {
    try {
      SwingUtilities.invokeAndWait( new Runnable() {
        public void run() {
          JFrame frame = new JFrame( "TestFrame" );
          frame.getContentPane().setLayout( new BorderLayout(  ) );
          DefaultListModel listModel = new DefaultListModel();
          listModel.addElement("Jane Doe");
          listModel.addElement("John Smith");
          listModel.addElement("Kathy Green");
          final JList list = new JList( listModel );
          list.setSelectedIndex( 0 );
          frame.getContentPane().add( list, BorderLayout.CENTER );
          JButton clearSelectionButton = new JButton( "Clear selection" );
          frame.getContentPane().add( clearSelectionButton, BorderLayout.SOUTH );
          clearSelectionButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent aActionEvent ) {
              list.clearSelection();
            }
          } );
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          frame.pack();
          frame.setVisible( true );
        }
      } );
    } catch ( InterruptedException e ) {
    } catch ( InvocationTargetException e ) {
    }
  }
}
        Robin
        
- 36,233
 - 5
 - 47
 - 99
 
- 
                    EDIT: My bad it works there was another bug. for some reason clearSelection does not work. Nothing happens when I call the method (at least in a visible way). the same list element still stays highlighted. – Cemre Mengü Dec 30 '11 at 19:15
 - 
                    so my problem is that there is a valueChanged event for the list and I think clearSelection triggers it which leads to a null pointer exception. – Cemre Mengü Dec 30 '11 at 19:41
 
3
            
            
        How about:
SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jList.clearSelection();
        }
}
Another way:
    jButton1.addActionListener(
        (ActionListener)EventHandler.create(ActionListener.class, jList1, "clearSelection"));
        Joop Eggen
        
- 107,315
 - 7
 - 83
 - 138
 
- 
                    Yeah and I missed this method even I went through the list of methods one by one :) Thanks. Since Robin was first I will give him the check mark hope you dont mind (I am giving you an up though) – Cemre Mengü Dec 30 '11 at 19:21