Cosimo Sguanci
Cosimo Sguanci

Reputation: 1291

JTextField setText doesn't work

I have a JTextField and I would like to setText at runtime when a button is pressed and the filechooser returns the selected file. The problem is that the TextField is not updating when the file is selected.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jFileChooser1=new JFileChooser();
    jTextField1=new JTextField();
    jFileChooser1.showOpenDialog(myJFrame.this);
    File f=jFileChooser1.getSelectedFile();
    String filePath=f.getAbsolutePath();
    System.out.println(filePath);
    jTextField1.setText((filePath));
    jTextField1.setVisible(true);
    System.out.println(jTextField1.getText());

}

The debug logs return the right values, the file path name. Thanks.

Upvotes: 2

Views: 5745

Answers (3)

Young Emil
Young Emil

Reputation: 2276

Declare and instantiate jTextField1 in your default constructor or As a global variable. For example:

public class Main {

    JTextField jTextField1 = new JTextField();//declaring as global varible
    public Main(){
        //jTextField1 = new JTextField();//can be instantiated here if it is already declared as global variable
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jFileChooser1 = new JFileChooser();
        jFileChooser1.showOpenDialog(myJFrame.this);
        File f = jFileChooser1.getSelectedFile();
        String filePath = f.getAbsolutePath();
        System.out.println(filePath);
        jTextField1.setText((filePath));
        jTextField1.setVisible(true);
        System.out.println(jTextField1.getText());

    }
    //other methods and code goes here.
}

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109613

actionPerformed is handled on the event loop. Postpone the text operations.

Also there probably exists a created JTextField, that was added to the window. Here a new one is created, without adding it anywhere.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {         
            JFileChooser fileChooser = new JFileChooser();
            //jTextField1 = new JTextField();
            if (fileChooser.showOpenDialog(myJFrame.this) == JFileChooser.APPROVE_OPTION) {
                File f = fileChooser.getSelectedFile();
                String filePath = f.getAbsolutePath();
                jTextField1.setText((filePath));
                //jTextField1.setVisible(true);
            }
        }        
    });
}

Java 8:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    EventQueue.invokeLater(() -> {
            JFileChooser fileChooser = new JFileChooser();
            //jTextField1 = new JTextField();
            if (fileChooser.showOpenDialog(myJFrame.this) == JFileChooser.APPROVE_OPTION) {
                File f = fileChooser.getSelectedFile();
                String filePath = f.getAbsolutePath();
                jTextField1.setText((filePath));
                //jTextField1.setVisible(true);
            }
    });
}

Upvotes: 2

Eskir
Eskir

Reputation: 784

You need to call setVisible last. Also, if the field is already created and visible, try calling .repaint() and .validate() on it.

Upvotes: 0

Related Questions