首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionListener on JFileChooser on a JTabbedPane

ActionListener on JFileChooser on a JTabbedPane
EN

Stack Overflow用户
提问于 2013-10-11 12:10:09
回答 1查看 390关注 0票数 1

我正在尝试从JFileChooser中加载一个文件,该文件位于JTabbedPane中。我希望文件加载在‘打开’按钮的点击,也显示在一个不同的窗格中的文件。我希望我能解释清楚。我有一个ActionListener,并尝试了一些东西,但我使用的代码似乎没有触发,因为它甚至不会打印到控制台。请你看看我的代码,看看我哪里出错了。谢谢

代码语言:javascript
复制
class listener implements ActionListener{
        public void actionPerformed (ActionEvent e)
        {.......// other actions

    else if (e.getSource() instanceof JFileChooser){
            JFileChooser openFile = (JFileChooser)e.getSource();
            String command = e.getActionCommand();
            if (command.equals(JFileChooser.APPROVE_SELECTION)){
                File selectedFile = openFile.getSelectedFile();
                System.out.print("test if working");
                tp.setSelectedIndex(0); //Index of JTab I wand file to load
                loadSavedGame(selectedFile);
                }
            else if (resume.equals(JFileChooser.CANCEL_OPTION)) {
                    //frame.setVisible(true);
                    tp.setSelectedIndex(0);
                }
           }
          }
         }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-11 12:17:01

JFileChooser类有一个addActionListener(...)方法,它将接受上面的ActionListener。它不需要显示为弹出式,它的工作。

您从未告诉我们是否或如何将上面的ActionListener添加到JFileChooser中,但是如果您实际上正在这样做,而且您的代码仍然不工作,那么您将希望创建和发布一个斯考斯,以供我们测试和修复。

编辑

此外,我将创建仅与ActionListener一起使用的JFileChooser,从而去掉这一行:

代码语言:javascript
复制
else if (e.getSource() instanceof JFileChooser){

如果监听器只添加到一个对象中,则不需要对源进行测试。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19317822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档