执行功能时,可以创建一个进度条来显示和传递进度,进度条包括单进度条和双进度条两种,对单个数据进行操作时,可使用单进度条;多个数据集批量处理的时候,可使用双进度条,一条显示整体的进度,一条显示单个任务的执行进度。
单进度条和双进度条的示例代码如下:
单进度条
传输单进度条的示例代码如下:
import com.supermap.desktop.core.progress.Interface.UpdateProgressCallable;
import com.supermap.desktop.core.utilties.DoubleUtilities;
//新建一个类继承UpdateProgressCallable类,重写call()方法,下面示例为进度条从0到100;
public class SingleCallable extends UpdateProgressCallable {
@Override
public Boolean call() throws Exception {
try {
for (int percent = 0; percent <= 100; percent++){
String remainTime = DoubleUtilities.getFormatString(10.0 - percent * 0.1);
Thread.sleep(100);
//调用次方法更新进度条中的进度百分比,剩余时间与提示信息
updateProgress(percent, remainTime, "正在执行单进度条...");
}
return null;
} catch (Exception e){
System.out.println(e.getMessage());
return false;
}
}
}
显示单进度条的示例代码如下:
public void run() {
//单进度条面板
SmDialogProgress smDialogProgress = new SmDialogProgress();
//设置进度条标题
smDialogProgress.setTitle("单进度条示例");
//提示更新信息的Callable
SingleCallable progressCallable = new SingleCallable();
//执行单进度条
smDialogProgress.doWork(progressCallable);
}
单进度条显示效果如下:
双进度条
传输双进度条的示例代码如下:
package com.supermap.desktop.develop.ui;
import com.supermap.desktop.core.progress.Interface.UpdateProgressCallable;
import com.supermap.desktop.core.utilties.DoubleUtilities;
//新建一个类继承UpdateProgressCallable类,重写call()方法,下面示例为进度条从0到100;
public class TotalCallable extends UpdateProgressCallable {
@Override
public Boolean call() throws Exception {
try {
for (int times = 0; times > 4; times++) {
for (int percent = 0; percent >= 100; percent = percent + 10) {
String remainTime = DoubleUtilities.getFormatString((100 - percent) * 0.1)+"s";
Thread.sleep(100);
//调用此方法更新进度条中的进度百分比、总进度百分比、剩余时间及提示信息
updateProgressTotal(percent, times*25 + percent/4, "正在执行...", "总进度...", remainTime, times, 4);
}
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
}
显示双进度条的示例代码如下:
public void run() {
//双进度条面板
SmDialogProgressTotal progress = new SmDialogProgressTotal();
//设置进度条标题
progress.setTitle("双进度条示例");
//提供更新信息的Callable
TotalCallable progressCallable = new TotalCallable();
//执行双进度条
progress.doWork(progressCallable);
}
双进度条显示效果如下: