[英]JFileChooser and JTextField connect
I have a JTextField
and a JFileChooser
. 我有一个JTextField
和一个JFileChooser
。 In the file chooser I want to select a file and then display it in the text field. 我要在文件选择器中选择一个文件,然后在文本字段中显示它。 Unfortunately this does not work. 不幸的是,这不起作用。 Can one any help me? 可以帮我吗?
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
jFileChooser1 = new JFileChooser();
int value = jFileChooser1.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPTION) {
File selectedFile = jFileChooser1.getSelectedFile();
}
}
textField.setText(selectedFile.getPath())
? textField.setText(selectedFile.getPath())
吗?
As I'm sure, by now, you're aware, JTextField#setText
expects a String
, so you need to use one of the File
methods to generate a String
representation of the File
object. 我确定,到目前为止,您已经知道JTextField#setText
需要一个String
,因此您需要使用File
方法之一来生成File
对象的String
表示形式。
If you don't want the full path/name of the File
, you could also just use File#getName
, which returns just the name of the File
without the path 如果您不想要File
的完整路径/名称,也可以使用File#getName
, File#getName
返回不带路径的File
的名称。
Have a closer look at the File
JavaDocs for more details 详细了解File
JavaDocs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.