Home » Java » JFileChooser throws IndexOutOfBoundsException while setSelectedFile

JFileChooser throws IndexOutOfBoundsException while setSelectedFile

Posted by: admin October 26, 2017 Leave a comment

Questions:

I’m working with JFileChooser and it throws unexpected IndexOutOfBoundsException while setting selected file. What can be wrong?

File result= null;

        _fileChooser.setDialogTitle(pDialogTitle);
        _fileChooser.setMultiSelectionEnabled(false);
        _fileChooser.setCurrentDirectory(new File(pDirectory));
        _fileChooser.setSelectedFile(new File(pDirectory,pDefaultFileName));

        if ((JFileChooser.APPROVE_OPTION == _fileChooser.showSaveDialog(null)))
        {
            result = _fileChooser.getSelectedFile();
        }

        return result;

java.lang.IndexOutOfBoundsException: Invalid index at
javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:514)
at sun.swing.FilePane$SortableListModel.getElementAt(FilePane.java:668)
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1360)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
at javax.swing.plaf.basic.BasicListUI$Handler.valueChanged(BasicListUI.java:2623)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.moveLeadSelectionIndex(DefaultListSelectionModel.java:777)
at sun.swing.FilePane.clearSelection(FilePane.java:1729)
at sun.swing.FilePane.setFileSelected(FilePane.java:1576)
at sun.swing.FilePane.doSelectedFileChanged(FilePane.java:1606)
at sun.swing.FilePane.propertyChange(FilePane.java:1675)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.awt.Component.firePropertyChange(Component.java:8428)
at javax.swing.JFileChooser.setSelectedFile(JFileChooser.java:508)

Answers: