JFile Chooser 在使用前抛出错误,只是有时?

发布于 2024-10-13 07:37:54 字数 3481 浏览 3 评论 0原文

我创建一个 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();
    }
}

I create a JFile Chooser, and use .setCurrentDirectory(); to set the Directory to the root of my java project folder by passing a newFile("."); This seems to work fine sometimes, but other times it throws an error. This all happens while the program is loading, before any user input, so as far as I can tell it's completely random whether it happens or not. Here's the File Chooser related bits of my code:

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";
        }
    }
}

The exact error is:

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 irrelavent, but here's main just in case:

package Cannon;

//Creates the frame
public class Main {

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-10-20 07:37:54

这一切都在程序加载时发生,

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

有关详细信息,请阅读 Swing 教程中有关并发的部分。并查看任何演示创建 GUI 的正确方法的示例。

This all happens while the program is loading,

All code that affects the GUI should execute on the Event Dispatch Thread. The creation of the GUI should be wrapped in a SwingUtilities.invokeLater().

Read the section from the Swing tutorial on Concurrency for more information. And look at any of the examples that demonstrate the proper way to create the GUI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文