- someButton.addSelectionListener( new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- IRunnableWithProgress runnable = new IRunnableWithProgress () {
- final byte [][] result = new byte [ 1 ][];
- //实现接口中的execute方法:
- protected void execute(IProgressMonitor monitor) throws CoreException {
- //具体的业务逻辑:
- }
- //实现接口中的run方法,该方法是一个同步方法:
- public synchronized final void run(IProgressMonitor monitor)
- throws InvocationTargetException, InterruptedException {
- try {
- //总的工作量
- int totalWork = IProgressMonitor.UNKNOWN;
- monitor.beginTask("A Progress monitor dialog example..." , 1000 );
- if (result[ 0 ] == null ){
- totalWork = 400 ;
- }
- monitor.worked(totalWork);
- //执行业务逻辑:
- execute(monitor);
- if (result[ 0 ] != null ) {
- totalWork = 1000 ;
- }else {
- totalWork = 700 ;
- }
- monitor.worked(totalWork);
- } catch (CoreException e) {
- throw new InvocationTargetException(e);
- } catch (OperationCanceledException e) {
- throw new InterruptedException(e.getMessage());
- }finally {
- monitor.done();
- }
- }
- };
- try {
- new ProgressMonitorDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell()).run( true , true , runnable);
- } catch (InvocationTargetException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
JFace进度条
最新推荐文章于 2012-11-02 16:32:30 发布
本文介绍如何使用Java实现异步任务,并通过进度监控对话框展示任务执行过程。具体包括创建一个自定义的RunnableWithProgress接口实现类,用于执行任务和更新进度,以及使用ProgressMonitorDialog来显示进度对话框。通过代码示例展示了任务执行的流程,包括任务初始化、工作量计算、业务逻辑执行和最终结果展示。
摘要由CSDN通过智能技术生成