Reputation: 67
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?
Upvotes: 0
Views: 1175
Reputation: 11
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);
}
});
Upvotes: 0
Reputation: 5055
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.
Upvotes: 1
Reputation: 57381
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?
Upvotes: 3