【问题标题】:Embedding JFileChooser嵌入 JFileChooser
【发布时间】:2019-12-22 04:15:13
【问题描述】:

我正在尝试添加一个JFileChooser,它选择一个父目录并允许用户输入文件名。我知道showSaveDialogshowOpenDialog 方法,但是我不想创建一个新窗口。

这是我目前所拥有的:

public class BrowserTest extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BrowserTest frame = new BrowserTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public BrowserTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JFileChooser browser = new JFileChooser();
        browser.setFileFilter(new FileFilter() {
            @Override
            public String getDescription() {
                return "A .extension file";
            }
            @Override
            public boolean accept(File f) {
                return f.isDirectory();
            }
        });
        browser.setDialogType(JFileChooser.SAVE_DIALOG);
        browser.setSelectedFile(new File("*.extension"));
        browser.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                final String action = e.getActionCommand();
                if(action.equals(JFileChooser.APPROVE_SELECTION)) {
                    System.out.println("trigger");
                    File f = browser.getCurrentDirectory();
                    System.out.println(f);
                }
                if(action.equals(JFileChooser.CANCEL_SELECTION))
                    JOptionPane.showConfirmDialog(null, "Start without saving?", "No save directory selected.", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            }
        });
        contentPane.add(browser);
        revalidate();
    }
}

由于某种原因,如果我选择一个文件,按下保存按钮只会执行System.out.println("trigger");。有没有办法直接听保存按钮按下?

【问题讨论】:

  • 你能发布一个有效的minimal reproducible example 程序吗?
  • @HovercraftFullOfEels 我编辑了示例,现在应该可以执行了。
  • 一大堆:您可以递归地遍历 JFileChooser 中保存的所有组件,找到感兴趣的按钮(此处使用“打开”的 ActionCommand),然后以这种方式添加侦听器。
  • 我想这可能会奏效。我试试看。

标签: java swing file-io jfilechooser


【解决方案1】:

您可以使用以下代码访问文件选择器的默认按钮,然后将您自己的侦听器添加到该按钮:

JButton defaultButton = browser.getUI().getDefaultButton(browser);

【讨论】:

    【解决方案2】:

    如前所述,一种解决方案是递归地遍历 JFileBrowser 的组件,直到找到正确的组件,这里是一个带有操作命令字符串“Open”的 JButton。

    例如,这个方法可以工作:

    public static void recursiveComponentSearch(Component c, String actionCommand,
            ActionListener listener) {
        if (c instanceof JButton) {
            JButton button = (JButton) c;
    
            // TODO: delete the line below
            System.out.printf("Text: \"%s\";  action command: \"%s\"%n", button.getText(),
                    button.getActionCommand());
    
            if (button.getActionCommand().equalsIgnoreCase(actionCommand)) {
                button.addActionListener(listener);
            }
        }
    
        // recursive search here
        if (c instanceof Container) {
            Container container = (Container) c;
            Component[] components = container.getComponents();
            for (Component component : components) {
                recursiveComponentSearch(component, actionCommand, listener);
            }
        }
    }
    

    类似:

    ActionListener listener = evt -> System.out.println("Save button here");
    String actionCommand = "Open";
    recursiveComponentSearch(browser, actionCommand, listener);
    

    【讨论】:

      【解决方案3】:

      我刚刚发现问题出在browser.setSelectedFile(new File("*.extension")); 行。显然,这种方法不喜欢星号,用其他任何东西替换它可以解决问题。例如:browser.setSelectedFile(new File("new.extension")); 将按预期工作。

      【讨论】:

        猜你喜欢
        • 2010-09-20
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多