Java Swing-Finder文本框

Java Swing-Finder文本框,java,swing,jfilechooser,Java,Swing,Jfilechooser,我现在有一个JFrame应用程序,它使用JTextField输入文件位置,如下代码所示 txtSource = new JTextField(); txtSource.setToolTipText("/location/of/file/test.txt"); txtSource.setText("/location/of/file/test.txt"); txtSource.setBounds(16, 122, 412, 29); contentPane.

我现在有一个JFrame应用程序,它使用JTextField输入文件位置,如下代码所示

    txtSource = new JTextField();
    txtSource.setToolTipText("/location/of/file/test.txt");
    txtSource.setText("/location/of/file/test.txt");
    txtSource.setBounds(16, 122, 412, 29);
    contentPane.add(txtSource);
    txtSource.setColumns(10);
我想做的是允许用户进行目录搜索,以选择本地计算机上文件的位置,该位置将填充到文本框中

我在JCHooser上找到了以下信息,但我不确定这是否是一条可行之路,并希望获得有关如何实施的帮助

String filename = File.separator+"tmp";
JFileChooser fc = new JFileChooser(new File(filename));

// Show open dialog; this method does not return until the dialog is closed
fc.showOpenDialog(frame);
File selFile = fc.getSelectedFile();

// Show save dialog; this method does not return until the dialog is closed
fc.showSaveDialog(frame);
selFile = fc.getSelectedFile();
提前感谢

使用

    int option = fc.showOpenDialog(frame);
    if (option == JFileChooser.APPROVE_OPTION) {
            txtSource.setText(fc.getSelectedFile().getAbsolutePath())
    }

用所选文件绝对文件位置填充文本字段

如果您只希望用户能够选择目录,则应向
JFileChooser.directories\u only
提供
JFileChooser.setFileSelectionMode
。这将导致选择器仅显示目录-如果您正试图这样做;)工作得很好。为此干杯。