1

Let's say I have a JTextField "status" and I'm running this code:

status = new JTextField(50);
add(status);

for (int i=0; i<10000; i++) {
     status.setText("bla bla - "+ i);
     System.out.println("bla bla - "+ i);
}

My problem is that right now while the loop is running nothing happened in the JTextField's text and only when the loop end the label is "bla bla - 10000".

I want to make something like a status bar but cant update this status bar "online". I also tried to do the update in a thread but ended with the same result.

Can someone show my how I can present a text in a GUI while iterating or looping?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chiko
  • 606
  • 9
  • 21
  • 2
    Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead implement a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Apr 15 '13 at 08:43
  • [This](http://stackoverflow.com/questions/6774414/why-is-my-jtextarea-not-updating) should help. – Kazekage Gaara Apr 15 '13 at 08:44
  • I would do it this way: http://nenadbulatovic.blogspot.com/2014/05/java-changing-jtextfields-text-while.html – Nenad Bulatović May 15 '14 at 19:15

4 Answers4

7

Use a SwingWorker to split UI-update and long running tasks.

Take a few minutes to read the end of the Swing tag wiki and follow the provided links.

Here is a small example of such code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class TestSwingWorker {

    private JTextField progressTextField;

    protected void initUI() {
        final JFrame frame = new JFrame();
        frame.setTitle(TestSwingWorker.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Clik me to start work");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                doWork();
            }
        });
        progressTextField = new JTextField(25);
        progressTextField.setEditable(false);
        frame.add(progressTextField, BorderLayout.NORTH);
        frame.add(button, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    protected void doWork() {
        SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
            @Override
            protected Void doInBackground() throws Exception {
                for (int i = 0; i < 100; i++) {
                    // Simulates work
                    Thread.sleep(10);
                    publish(i);
                }
                return null;
            }

            @Override
            protected void process(List<Integer> chunks) {
                progressTextField.setText(chunks.get(chunks.size() - 1).toString());
            }

            @Override
            protected void done() {
                progressTextField.setText("Done");
            }
        };
        worker.execute();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestSwingWorker().initUI();
            }
        });
    }
}
Community
  • 1
  • 1
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
3

Use a javax.swing.Timer. Here is an example that shows you how:

private void refreshMyTextField() {       
    Timer timer1 = new Timer(100, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            jTextField1.setText("bla bla - "+(j++));
        }
    });
    timer1.start();  
}
Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
0

Use Timer class which executes a task in intervals. Because you don't use interval, what you get is the last value 10000. You can't see previous values because of interval absence.

Regards,

Ravi Trivedi
  • 2,340
  • 2
  • 14
  • 20
0

try to use status.setText(""); before status.setText("bla bla - "+ i);

komal kadam
  • 185
  • 1
  • 1
  • 11