user5201464
user5201464

Reputation:

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();
    }
}                                     

Upvotes: 1

Views: 243

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

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

Upvotes: 1

Related Questions