user2620625
user2620625

Reputation:

Using Threads with Java Swing

I'm building a user-interface with Java's swing library, and I've run into a problem.

When updating a text-area in a long method, there is a pause while the method runs, and then the text area is updated all at once, instead of a little bit at a time.

I managed to fix the problem using a new thread in the following manner:

private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Thread x = new Thread() {public void run() {
    // do things that update a text area
    }}; x.start();
}

This works perfectly for having the text area update in small pieces, as opposed to all at once.

However, the only problem is that this method can only be called once or Java crashes. I read that this has to do with only being able to create the thread one time. How can I modify the code to keep the desired functionality?

Any help would be greatly appreciated.

Upvotes: 0

Views: 86

Answers (3)

Kishan Bheemajiyani
Kishan Bheemajiyani

Reputation: 3439

Well you can make new object of the Thread and override all the method means runnable.

new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println("Executed..*****");
            }
        }).start();

try this.

Upvotes: 0

Gorbles
Gorbles

Reputation: 1168

Just for comparison's sake, here is an example using SwingUtilities.invokeLater. I echo MadProgrammer's advice though, learning about SwingWorker is much more beneficial in the long term and gives better variable handling.

newGame.addActionListener(new ActionListener() {
    @Override
        public void actionPerformed(final ActionEvent arg0) {
            panelList.get("newGame").setEnabled(false);     
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // this is where the heavy processing occurs
                    newGameButton();
                    // re-enables the button afterwards
                    panelList.get("newGame").setEnabled(true);
                }
            });             
        }
});

Also, once your code is working better, you can probably look into ways to optimise the speed of the update (even when dealing with Swing objects). The above call used to hang for a second or so, but I've got the delay to about 50 - 100ms or so with further work on the model :)

Upvotes: -1

MadProgrammer
MadProgrammer

Reputation: 347184

Well, I doubt you codes crashing because of the Thread creation, because each time the method is called, you're creating a new Thread (you can't restart an existing instance of a Thread that has already been started (even if it's completed or not)), the likely cause is you're getting a concurrent modification exception because Swing is not Thread safe...

Probably the easiest solution would be to use a SwingWorker which will allow you to execute your long running task in the background, but provide easy to use functionality to update the UI safely, through the publish, process and done methods.

Do prevent possible issues, you might also consider disabling the button until the load action is completed, which will prevent people from mashing the button spawning multiple background processes...

Upvotes: 4

Related Questions