Hello.

I've got such example statement:

for( int i=0; i <= 10000; i++ )
	myTextArea.setText( myTextArea.getText()+ i + "\n");

What is the problem? I can't figure it out, why in every iteration of loop the text area isn't refreshing with new String added.

When this loop is iterating, app freezes and a complete text shows after the loop ends.

Why is this happening?

How to modify it, so by every iteration I could see result on text area, as it should be?

Recommended Answers

All 7 Replies

I'm getting to read then. Good to get to know something new. I'll mark thread as solved, if I'll get it working.

One technical issue and question about that.

In that way every click on button (to print on text area) creates new thread. I thought about that how to fix it and I made that:

class MyFrame extends JFrame
{
    private boolean printingFinished = true;

    (...)

    public void setPrintingFinished(boolean b)
    {
        printingFinished = b;
    }

    public void buttonToPrintClicked(ActionEvent e)
    {
        if( printingFinished )
        {
            new Thread( new Runnable()
            {
                public void run()
                {
                    setFinish(false);
                    for( int i=0; i < 1000; i++ )
                        myTextArea.setText( myTextArea.getText()+i );
                    setFinish(true);
                }
            }).start();
        }
    }
    (...)
}

Is this the right way?

Looks reasonable, however you probably want to sleep for a bit between each update and you may want to ensure that your updates to the text area are being made on the event thread

public void run() {
            try {
                setFinish(false);

                for (int i = 0; i < 1000; i++) {
                    // queue the text update
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            myTextArea.setText(myTextArea.getText() + i);
                        }
                    });
                    // sleep for a bit so repaint and other stuff can process
                    Thread.sleep(100);
                }

                setFinish(true);               
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }

A fewof quick comments:
I wouldn't worry about creating a new thread for each click - what's the problem?
You shouldn't need to run the text update on the EDT, setText is one of the very few swing methods that is thread safe.
Rather than sleeping your thread, just call yield() so there's no delay. You may need to set your thread's priority below that of the EDT.

(sorry - pressed Post button twice!)

A fewof quick comments:
I wouldn't worry about creating a new thread for each click - what's the problem?

The problem was the action that button performes is very time consuming, so that UI was unavaible at that time.


BTW: I found a solution to my previous question (in Core Java 2 Advanced book), which looks more reasonable:

// action performed
if( buttonThread == null )
{
    buttonThread = new Thread(new Runnable()
    {
        public void run()
        {
            for( int i=0; i<10000; i++ )
                myTextArea.setText( myTextArea.getText() + i + "\n" );

            buttonThread = null;
        }
    } );

    buttonThread.start();
}

(...)

private Thread buttonThread;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.