felipedz
felipedz

Reputation: 97

When I invoke a JFrame and a process Callable from another JFrame, freezes the GUI

I have two jframes: WinA and WinB, also i have a class Callable to do a process.

WinA have a button to do a process in thread with Callable and show a WinB witha a progressbar.

WinA class code - ActionPerformed of button.

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            WinA wa = new WinA();
            WinB wb = new WinB();
             ClaseCallable callbd = new ClaseCallable();
             ExecutorService exesrv = Executors.newSingleThreadExecutor();
             Future sresp;
             sresp = exesrv.submit(callbd);
             wb.getProgressbar().setIndeterminate(true);
             wb.setVisible(true);
             System.out.println(">>" + sresp.get());
             exesrv.shutdown();
             wb.setVisible(false);
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger(WinA.class.getName()).log(Level.SEVERE, null, ex);
        }
    }          

ClaseCallableclass code

public class ClaseCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        for(int i=0; i<10; i++){
            Thread.sleep(250);
        }
        return 33;
    }

}

When I run from WinA and pressed the button opens the WinB, but the window is white, at the end shows the result.

Do not understand why this happens, if it is performing and swing events in EDT thread and process in another thread.

Upvotes: 0

Views: 171

Answers (1)

John Gardner
John Gardner

Reputation: 25146

You're calling future.get() which will block until the task completes:

from http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html#get()

get V get() throws InterruptedException, ExecutionException

Waits if necessary for the computation to complete, and then retrieves its result.

Upvotes: 1

Related Questions