java ui 多线程,Java中的多线程,我希望UI和代码都可以并行执行

I have a JFrame with progress bar and my code which should run it in background showing the progress in progress bar.

I implemented runnable in my Progressbar class and started the thread. But the progress bar frame is not displaying full.. and it gets stuck and it display full after my code is fully executed i.e after main thread close.

I know this is some basic mistake.

public class ProgressScriptUI extends JFrame implements Runnable{

@Override

public void run() {

// TODO Auto-generated method stub

setTitle("Progressing to Generate DDL Scripts");

setBounds(400, 250, 850, 400);

getContentPane().setLayout(null);

JProgressBar progressBar= new JProgressBar(0,100);

progressBar.setBounds(200, 100, 500, 20);

add(progressBar);

setVisible(true);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

//I am calling the below code in some other class

ProgressScriptUI progress = new ProgressScriptUI();

Thread uiThread = new Thread(progress);

uiThread.start();

oracleValidateOLDorNEW.execute(); //Code that I need to call in back ground

解决方案

All UI code must be executed within the context of the Event Dispatching Thread. This makes it a little tricky to do work in the background while keeping the UI up to date.

Take a look at Concurrency in Swing for suggestions and recommendations for solutions.

Probably the easiest would be to use a SwingWorker which has functionality to publish/process data so that it is synced back to the EDT as well as progress callbacks

You can take a look at

As some examples....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值