JLabel and JTextField setText is not update

我的梦境 提交于 2019-12-02 18:09:37

问题


I want to update jlabel and jtextfield with setText() method but it is not working. However, rest of the code is working. The code is below;

btnDosyaSe.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser jfc = new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

                int kullaniciSecimi = jfc.showOpenDialog(null);
                if (kullaniciSecimi == JFileChooser.APPROVE_OPTION) {
                    File fileName = jfc.getSelectedFile();
                    textField.setText(fileName.getPath());
                    islemSureci.setText("Veriler Okunuyor...");

                    try {

                        ArrayList<ArrayList<String>> tumYazılar = rwd.readTXT(fileName.getPath());
                        String[] yazarlar = rwd.yazarlar(fileName.getPath());
                        islemSureci.setText("Veriler Okundu! Öznitelik çıkarımına başlandı...");

                        oznitelikler = oc.oznitelikleriBul(tumYazılar, yazarlar);
                        islemSureci.setText("Öznitelikler Çıkarılmıştır!");
                        //String text = readTXT(fileName);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

In here, islemSureci is JLabel and textFiled is JTextField. When they are set, it is not working. After all the work finish in the code, they appear. Please can you tell what is the reasons?


回答1:


I think you mean by "After all the work finish" is loading text from the file(the line you commented). I think this code works fine because you don't do nothing after setting the text. You can use another thread to load text, if it is true.

If oc.oznitelikleriBul() takes a long time, you can code that line and text setting line in a different Thread. But, in that case make sure to run text setting line in an EDT inside the new Thread.




回答2:


Your long running task is executed in Event Dispatcher Thread which locks the thread where update should be performed. Thus the real update is perfomed only after the task is finished.

Run it in SwingWorker

See e.g.

http://www.javacreed.com/swing-worker-example/

or

How do I use SwingWorker in Java?




回答3:


I had same issue. How I solved see below :

I have 2 JFrame classes. Login.java and Employee.java. After login I can not setText() to JTextField object in Employee Form. So what I did ... I used setVisible() employee form with this code. Now I can refresh my JTextField.

Thanks and I hope it will resolve the issues.

java.awt.EventQueue.invokeLater(new Runnable() {
   public void run() {
        new Employee().setVisible(true);
   }
});


来源:https://stackoverflow.com/questions/27604470/jlabel-and-jtextfield-settext-is-not-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!