简体   繁体   English

JFileChooser和JTextField连接

[英]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#getNameFile#getName返回不带路径的File的名称。

Have a closer look at the File JavaDocs for more details 详细了解File JavaDocs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JFileChooser设置值JTextField - Using JFileChooser to set values JTextField 在JFileChooser中获取“打开”按钮和JTextField? - Getting the Open button and JTextField in a JFileChooser? JFileChooser在JTextField中显示多个选定的文件 - JFileChooser show multiple selected files in JTextField Java:如何在JTextField中显示由JFileChooser打开的文件的内容? - Java: How to show the content of a file, opened by JFileChooser, in a JTextField? 自定义javax.swing.JFileChooser以包含其他JTextField - Customizing javax.swing.JFileChooser to include an additional JTextField 如何通过JFileChooser将所有选定的文件绝对路径/名称设置为JTextField - How to set all selected file absolute paths/names by JFileChooser into a JTextField 改变JFileChooser行为:阻止在文件路径JTextField中输入“choose” - altering JFileChooser behaviour : preventing “choose” on enter in file path JTextField 使用all(jComboBox,JTextField,jFileChooser)作为表编辑器会覆盖引用 - Using all (jComboBox, JTextField, jFileChooser) as table editor overrides the refrences 选择带有JFileChooser的txt文件并使其出现在JTextField上是行不通的 - Choosing a txt file with JFileChooser and making it appear on a JTextField just won't work 如何将组合框(字符串)连接到JFrame for Java上的JTextField - How to connect combobox (String) to a JTextField on JFrame for Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM