0

My Open button in my DefaultForm JFrame has this Listener

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFileChooser chooser = new JFileChooser();
    File file = null;
    int returnVal = chooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        System.out.println(file.getName());
    }
    try {
        Scanner sc = new Scanner(file);
        while (sc.hasNext()) {
            String[] s = sc.nextLine().split(",");
            digitalPanel.tempTextField.setText(s[0]);
            digitalPanel.airTextField.setText(s[1]);
            digitalPanel.insoTextField.setText(s[2]);
            Thread.sleep(100);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(DefaultForm.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(DefaultForm.class.getName()).log(Level.SEVERE, null, ex);
    }

}  

digitalPanel is a panel in the DefaultForm.

I want to display data read from a file but with an amount delay time.

It will work if i remove Thread.sleep(1000);

But it gets stuck when i put Thread.sleep(1000);in the while loop

Got any ideas in this case ?

Your help will be appreciated.

Forrest
  • 723
  • 2
  • 8
  • 24
  • 1
    `It will work if i remove Thread.sleep(1000); But it gets stuck Thread.sleep(1000);` this doesnt makes sense... explain – Sir. Hedgehog Aug 22 '16 at 13:05
  • 1
    did you try debugging to check if it gets "stuck" or if it just needs more time to execute? try adding some print outs, and check the result.... – Sir. Hedgehog Aug 22 '16 at 13:09
  • Maybe you add `System.out.println(s[0])` in your while loop to see if it really gets stuck. – CloudPotato Aug 22 '16 at 13:11
  • It doesn't get stuck. It just freezes the `EDT` preventing any changes from showing up in the UI. – Kayaman Aug 22 '16 at 13:15
  • @Blobonat well, it can print s[0] but i don't know why it does not set text to my textfields. If i remove the line `Thread.sleep(1000)`, it will set text as i want but with the last value in the file – Forrest Aug 22 '16 at 13:16
  • if you are using textfields, then its another thing, point is not only to `setText.TextField1 = "whatever"`, point is after you set the text, to UPDATE the textfield so the information can be visible..... – Sir. Hedgehog Aug 22 '16 at 13:23
  • setText public void setText(String t) Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text. When text has been inserted, the resulting caret location is determined by the implementation of the caret class. Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use `DocumentListener`. – Sir. Hedgehog Aug 22 '16 at 13:24
  • 1
    Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Aug 22 '16 at 13:49
  • thank you. http://stackoverflow.com/questions/7251675/how-to-update-a-jlabel-every-time-with-a-while-loop-with-a-delay This link helps me – Forrest Aug 22 '16 at 14:01

0 Answers0