Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将文件从FileChooser转换为字符串_Java_File Io_Jfilechooser_Typeconverter - Fatal编程技术网

在Java中将文件从FileChooser转换为字符串

在Java中将文件从FileChooser转换为字符串,java,file-io,jfilechooser,typeconverter,Java,File Io,Jfilechooser,Typeconverter,这是我第一次在Java中使用JFileChooser,我目前正在尝试将所选文件夹从文件类型转换为字符串,这样我就可以将其自动写入GUI上的JTextField,以便用户可以确认他们所选的内容 我正在处理的代码部分如下: JButton btnSel1 = new JButton("Select..."); btnSel1.addActionListener(new ActionListener() { public void actionPerformed(ActionE

这是我第一次在Java中使用JFileChooser,我目前正在尝试将所选文件夹从文件类型转换为字符串,这样我就可以将其自动写入GUI上的JTextField,以便用户可以确认他们所选的内容

我正在处理的代码部分如下:

JButton btnSel1 = new JButton("Select...");
    btnSel1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int returnVal = fc.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                fol1 = fc.getFileAsString(file);
                rawIn1.setText(fol1);
            }   
        }
    });
    welFrame.getContentPane().add(btnSel1, "6, 6, center, default");
我希望变量“fol1”(文件夹1)写入JTextField“rawIn1”

getFileAsString方法返回错误“未定义JFileChooser类型”

提前感谢所有为我检查过这个的人


Jared。

如果只需要文件名的最后一部分,您可能需要使用
File.getPath()
File.getName()

您可能需要使用
File.getPath()
File.getName()
如果只需要文件名的最后一部分。

JFileChooser
中没有
getFileAsString
方法。你应该使用

rawIn1.setText(file.getName());

如图中所示:


JFileChooser
中没有
getFileAsString
方法。你应该使用

rawIn1.setText(file.getName());

如图中所示:


没有这样的方法,这就是它告诉你的。文件选择器有一个打开(/save)和*取消按钮。为什么用户需要再次确认文件选择?没有这样的方法,这就是它告诉您的。文件选择器有一个打开(/save)和*取消按钮。为什么用户需要再次确认文件选择?
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());
}