java - JFileChooser 在使用前抛出错误,只是有时?

标签 java swing jfilechooser

我创建了一个 JFile Chooser,并使用 .setCurrentDirectory();通过传递 newFile(".") 将目录设置为我的 java 项目文件夹的根目录;这似乎有时工作正常,但有时它会抛出错误。这一切都发生在程序加载时,在任何用户输入之前,据我所知,无论是否发生,这都是完全随机的。这是我的代码中与文件选择器相关的部分:

public class PnlHighScores extends JPanel {

    JFileChooser fcScores = new JFileChooser();

    PnlHighScores() {

        fcScores.addChoosableFileFilter(new TxtFilter());

        //***********This seems to cause a strange error only somethimes, Right as the program is run!***********
        fcScores.setCurrentDirectory(new File("."));//http://www.rgagnon.com/javadetails/java-0370.html
    }


    class ActFileChooser implements ActionListener {

        public void actionPerformed(ActionEvent e) {//http://download.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemoProject/src/components/FileChooserDemo.java
            int returnVal = fcScores.showOpenDialog(PnlHighScores.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                filScores = fcScores.getSelectedFile();
                sFileLocation = filScores.getAbsolutePath();//.getParent();//http://www.java-forums.org/awt-swing/29485-how-retrieve-path-filechooser.html
                //System.out.println(filScores);
                pnlScoreText.updateScoreFile(sFileLocation);
            }

        }
    }
    class TxtFilter extends javax.swing.filechooser.FileFilter {//http://www.exampledepot.com/egs/javax.swing.filechooser/Filter.html

        public boolean accept(File file) {
            String filename = file.getName();
            if (file.isDirectory()) {
                return true;
            } else {
                return filename.endsWith(".txt");
            }
        }

        public String getDescription() {
            return "*.txt";
        }
    }
}

准确的错误是:

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

Main类只是简单地创建了FraWindow,FraWindow通过其构造方法创建了PnlSetting。它们应该是无关紧要的,但为了以防万一,下面是主要的:

package Cannon;

//Creates the frame
public class Main {

    public static void main(String[] args) {
        FraWindow fraMain = new FraWindow();
    }
}

最佳答案

This all happens while the program is loading,

所有影响 GUI 的代码都应该在事件调度线程上执行。 GUI 的创建应该包含在 SwingUtilities.invokeLater() 中。

阅读 Concurrency 上的 Swing 教程部分了解更多信息。并查看任何演示创建 GUI 的正确方法的示例。

关于java - JFileChooser 在使用前抛出错误,只是有时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4766227/

相关文章:

Java Applet JFileChooser 按钮问题?

java - 为什么我的 HTTPPost 收到 404 响应?

java - 创建自定义 JFileChooser

java - Spring MVC 上传文件,然后提供下载链接

java - 如何将 ArrayList 绑定(bind)到 JList

java - Swing主动渲染效率或如何将主动渲染与gui widgets结合起来

java - 如何禁用 JScrollPane 上滚轮滚动事件的默认绘制行为

java - 如何将字符串转换为文件名

java - 不丢弃数据包的距离 vector 线程

java - 具有角色和权限的 Spring Security