0

我正在制作一个文件打开向导,并从初始窗口上的浏览按钮打开 JFileChooser。我目前已对其进行了设置,以便浏览按钮处理第一个窗口并同时打开 JFileChooser 窗口。我宁愿在用户选择他们的文件处理窗口,以防他们想要取消并返回初始窗口 - 目前这是不可能的。

以下是相关代码:

class BrowseButton extends JButton {

    public BrowseButton(String name, final JPanel pane) {

        super(name);
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JFileChooser fileopen = new JFileChooser();             
                FileFilter filter = new FileNameExtensionFilter("dwg files", "dwg");
                fileopen.addChoosableFileFilter(filter);

                int ret = fileopen.showDialog(pane, "Open");

                if (ret == JFileChooser.APPROVE_OPTION) {
                    File file = fileopen.getSelectedFile();
                    String[] layers = getFileLayers(file.getPath());
                    openLayerWindow(layers);
                }

            }
        });
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                dispose();
            }
        });
    }

当按钮被实例化时......

//Bottom Panel
    final JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));

    BrowseButton browse = new BrowseButton("Browse...", bottom);
    browse.setMnemonic(KeyEvent.VK_B);
    CloseButton close = new CloseButton("Close");
    close.setMnemonic(KeyEvent.VK_C);

    bottom.add(close);
    bottom.add(browse);
    basic.add(bottom);
4

1 回答 1

2

您可以利用SwingUtilities.getWindowAncestor检索您的包含窗口BrowseButton并仅在用户选择APPROVE_OPTION.

class BrowseButton extends JButton {

    public BrowseButton(String name, final JPanel pane) {

        super(name);
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JFileChooser fileopen = new JFileChooser();             
                FileFilter filter = new FileNameExtensionFilter("dwg files", "dwg");
                fileopen.addChoosableFileFilter(filter);

                int ret = fileopen.showDialog(pane, "Open");

                if (ret == JFileChooser.APPROVE_OPTION) {
                    SwingUtilities.getWindowAncestor(BrowsButton.this).dispose();
                    File file = fileopen.getSelectedFile();
                    String[] layers = getFileLayers(file.getPath());
                    openLayerWindow(layers);
                }

            }
        });
    }
于 2013-07-08T17:57:48.550 回答