简体   繁体   中英

JFileChooser and JTextField connect

I have a JTextField and a 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()) ?

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.

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

Have a closer look at the File JavaDocs for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM