Search code examples
javaswingjlabeljtextfield

JLabel and JTextField setText is not update


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?


Solution

  • 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.