I create a JFile Chooser and use .setCurrentDirectory (); set the directory to the root folder of my java project by passing newFile ("."); This seems to work fine sometimes, but in other cases it causes an error. All this happens during program loading until any user enters, as far as I can tell, it is an accident if this happens or not. Here in the Chooser file the bits of my code are linked:
public class PnlHighScores extends JPanel {
JFileChooser fcScores = new JFileChooser();
PnlHighScores() {
fcScores.addChoosableFileFilter(new TxtFilter());
fcScores.setCurrentDirectory(new File("."));
}
class ActFileChooser implements ActionListener {
public void actionPerformed(ActionEvent e) {
int returnVal = fcScores.showOpenDialog(PnlHighScores.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
filScores = fcScores.getSelectedFile();
sFileLocation = filScores.getAbsolutePath();
pnlScoreText.updateScoreFile(sFileLocation);
}
}
}
class TxtFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
String filename = file.getName();
if (file.isDirectory()) {
return true;
} else {
return filename.endsWith(".txt");
}
}
public String getDescription() {
return "*.txt";
}
}
}
Exact error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Invalid index
at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:497)
at sun.swing.FilePane$SortableListModel.getElementAt(FilePane.java:528)
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1343)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1294)
at javax.swing.plaf.basic.BasicListUI.getCellBounds(BasicListUI.java:935)
at javax.swing.JList.getCellBounds(JList.java:1600)
at javax.swing.JList.ensureIndexIsVisible(JList.java:1116)
at sun.swing.FilePane.ensureIndexIsVisible(FilePane.java:1540)
at sun.swing.FilePane.doDirectoryChanged(FilePane.java:1466)
at sun.swing.FilePane.propertyChange(FilePane.java:1513)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:339)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:276)
at java.awt.Component.firePropertyChange(Component.java:8128)
at javax.swing.JFileChooser.setCurrentDirectory(JFileChooser.java:568)
at Cannon.PnlSettings.<init>(PnlSettings.java:45)
at Cannon.FraWindow.<init>(FraWindow.java:19)
at Cannon.Main.main(Main.java:7)
Java Result: 1
The main class simply creates FraWindow, and FraWindow creates PnlSetting through its constructor method. They should be impartial, but the main thing is just in case:
package Cannon;
public class Main {
public static void main(String[] args) {
FraWindow fraMain = new FraWindow();
}
}
source
share