使用Java Swing选择文件路径并对该选择执行某些操作

Jus*_*tin 3 java swing jfilechooser file

我一直在javax.swing中阅读JFileChooser.*我知道showOpenDialog()方法将允许我选择一个文件并单击"选择"但我有一个特定的方式我希望它工作.

我想使用两个JFileChooser(可能在JPanel中并排)来选择TO和FROM路径,然后单击一个按钮,该按钮将从'Chooser'中获取用户输入并执行某些操作.

也许有人有一个像这样做一个JFileChooser的例子?基本上只是突出显示选择器中的文件/目录,然后单击某个OTHER按钮以从"选择器"(也是JFileChoosers按钮(取消和选择)中获取输入被隐藏).

很可能这个"其他"按钮只是代码的信号,以从JFileChooser对象获取值.

我希望自己是Swing的新手,还有另一个我失踪的课程可以做我所描述的课程,但它只是没有出现在谷歌搜索中我一直在制作.

Mad*_*mer 5

这是我的第一个传递(我在我的Mac上,所以我在使用JDK源时遇到了一些问题;))

问题是,摆脱cancelokay按钮......

在此输入图像描述

public class TestFileChooser2 {

    public static void main(String[] args) {
        new TestFileChooser2();
    }

    public TestFileChooser2() {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MainPane());
                frame.setSize(800, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });

    }

    protected class MainPane extends JPanel {

        private JFileChooser fileChooser;
        private JPanel filePane;

        private JTextField fileField;

        public MainPane() {

            setLayout(new BorderLayout());

            fileChooser = new JFileChooser();
            fileChooser.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equals("SelectedFileChangedProperty")) {
                        File file = fileChooser.getSelectedFile();
                        if (file != null) {
                            setFile(file);
                        }
                    }
                }
            });

            add(fileChooser, BorderLayout.WEST);

            filePane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            fileField = new JTextField(10);
            filePane.add(fileField, gbc);

            add(filePane);

        }

        protected void setFile(File file) {

            fileField.setText(file.getPath());

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

更新

显然Windows不喜欢与属性更改侦听器玩得很好...

在此输入图像描述

别搞错了,这是一个完整的黑客......

public class TestFileChooser2 {

    public static void main(String[] args) {
        new TestFileChooser2();
    }

    public TestFileChooser2() {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MainPane());
                frame.setSize(800, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });

    }

    protected class MainPane extends JPanel {

        private JFileChooser fileChooser;
        private JPanel filePane;
        private JTextField fileField;

        public MainPane() {

            setLayout(new BorderLayout());

            fileChooser = new JFileChooser();
            fileChooser.setApproveButtonText("delete");
            removeButtons(fileChooser);

            JList list = findFirstChildren(fileChooser, JList.class);
            list.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        File file = (File)((JList)e.getSource()).getSelectedValue();
                        if (file != null) {
                            setFile(file);
                        }
                    }
                }
            });

//            fileChooser.addPropertyChangeListener(new PropertyChangeListener() {
//                @Override
//                public void propertyChange(PropertyChangeEvent evt) {
//                    System.out.println(evt.getPropertyName());
//                    if (evt.getPropertyName().equals("SelectedFileChangedProperty")) {
//                        File file = fileChooser.getSelectedFile();
//                        if (file != null) {
//                            setFile(file);
//                        }
//                    }
//                }
//            });

            add(fileChooser, BorderLayout.WEST);

            filePane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            fileField = new JTextField(10);
            filePane.add(fileField, gbc);

            add(filePane);

        }

        protected void setFile(File file) {

            fileField.setText(file.getPath());

        }

        protected void removeButtons(Container container) {
            for (Component child : container.getComponents()) {

                if (child instanceof JButton) {
                    JButton btn = (JButton) child;
                    if (btn.getText() != null && (btn.getText().equals(fileChooser.getApproveButtonText()) || btn.getText().equals("Cancel"))) {
                        container.remove(child);
                    }
                } else if (child instanceof Container) {
                    removeButtons((Container) child);
                }

            }
        }

        public <T extends Component> T findFirstChildren(JComponent component, Class<T> clazz) {

            T child = null;
            for (Component comp : component.getComponents()) {

                if (clazz.isInstance(comp)) {

                    child = (T) comp;
                    break;

                } else if (comp instanceof JComponent) {

                    child = findFirstChildren((JComponent) comp, clazz);
                    if (child != null) {
                        break;
                    }

                }

            }

            return child;

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

一个更好的解决方案是FileSystemView直接利用并建立你自己的视图,但那是我现在有时间的努力:(

  • 另请参见使用`FileSystemView`的[示例](http://codereview.stackexchange.com/q/4446/6692). (2认同)

tra*_*god 5

示例扩展JFileChooser为通过覆盖批准和取消方法直接处理选择。

class MyChooser extends JFileChooser {

    @Override
    public void approveSelection() {
        ...
    }

    @Override
    public void cancelSelection() {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)