简体   繁体   English

JTextField setText不起作用

[英]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. 我有一个JTextField,我想在运行时通过按下按钮和filechooser返回选定的文件来设置setText。 The problem is that the TextField is not updating when the file is selected. 问题是选择文件时TextField没有更新。

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. 谢谢。

actionPerformed is handled on the event loop. actionPerformed在事件循环上处理。 Postpone the text operations. 推迟文本操作。

Also there probably exists a created JTextField, that was added to the window. 也可能存在一个创建的JTextField,它已添加到窗口中。 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: 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);
            }
    });
}

You need to call setVisible last. 您需要最后调用setVisible Also, if the field is already created and visible, try calling .repaint() and .validate() on it. 另外,如果已经创建现场可见,尝试调用.repaint().validate()就可以了。

Declare and instantiate jTextField1 in your default constructor or As a global variable. 在默认构造函数中或作为全局变量声明和实例化jTextField1 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.
}

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

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