0

Hi I am trying for an application that reads and writes into excel on clicking run JButton. I am using a JTextField to give status (started / completed). Everthing is going well except JTextField not being updated till the end.

Based on previous answers I tried following.
1)repaint JTextFieldvariable.
2)validate JTextFieldvariable.
3)validate JPanelvariable.
4)Tried to put process for sleep while till update happens.

Here is the code inside ActionPerformed method of run JButton

   private void RunButton_ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    status_val.setText("Started");
    status_val.repaint();
    status_val.validate();
    jPanel1.repaint();
    try {
        Thread.sleep(7000);
    } catch (InterruptedException ex) {
        Logger.getLogger(Intiall_Caller.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        //function to read and write into file
    } catch (Exception ex) {
        Logger.getLogger(Intiall_Caller.class.getName()).log(Level.SEVERE, null, ex);
    }
    status_val.setText("Completed");
}     

Edit 1: Just now I observed that the update on JTextField is not happening (delayed) till the end of ActionPerformed. All though I am not completely sure I think this could be the reason.

sql_dummy
  • 715
  • 8
  • 23
  • @JB NIzet my purpose using `sleep` itself is to look if could help in updating dynamically, or else I do not need sleep. I tried even without `sleep`. – sql_dummy Mar 06 '17 at 09:02
  • That doesn't change anything. While you're executing code inside actionPerformed, you're using the event dispatch thread, and thus prevent it from repaining the screen. So if you want to repaint the screen multiple times, the long code must be done in another thread. Use a SwingWorker. – JB Nizet Mar 06 '17 at 09:34
  • Now i tried removing `sleep` what I observed is the `JTextField` is updated after the file_read/write_method,or more specifically at the end of `ActionPerformed` method. I think `sleep` has nothing do with update or atleast in my case(code). – sql_dummy Mar 06 '17 at 09:39
  • It doesn't matter if you use sleep or something else what takes time. What matters is that you block the event dispatch thread. – Tom Mar 06 '17 at 13:31
  • Here Í got the solution as you guys suggested [SwingWorker](http://stackoverflow.com/questions/977740/swing-cant-get-jbutton-to-update-repaint-not-working?rq=1) – sql_dummy Mar 06 '17 at 17:00

0 Answers0