我想利用ProgressMonitor
的功能,但我不想让用户选择取消操作。我知道如果我不拨打isCanceled()
,那么按钮按下没有任何作用,但我不希望用户认为该程序没有响应。如何在使用javax.swing.ProgressMonitor时禁用取消按钮?
我该如何去做这件事?
我想利用ProgressMonitor
的功能,但我不想让用户选择取消操作。我知道如果我不拨打isCanceled()
,那么按钮按下没有任何作用,但我不希望用户认为该程序没有响应。如何在使用javax.swing.ProgressMonitor时禁用取消按钮?
我该如何去做这件事?
你不能。使用JProgressBar创建自己的对话框,如The Java Tutorial中所述。
第一个问题是为什么你不希望用户能够取消操作?如果需要那么长时间,他们为什么不能够决定他们不想等待呢?
如果你真的不想取消按钮,你必须咬紧牙关并创建你自己的进度对话框。你可以看看ProgressMonitor
的源代码,看看他们是怎么做的(他们使用JOptionPane
作为对话框并将其传递给几个Strings
和JProgressBar
)。
我不知道为什么所有ProgressMonitor字段都是私有的。也许不是Godsling先生的最值得骄傲的创作:)
* @author James Gosling
* @author Lynn Monsanto (accessibility)
* @version 1.37 04/12/06
就克隆它,从秋千一些包私人的东西一起,那么你可以做你想做的,像可取消添加标记,并在ProgressOptionPane的构造函数中使用它。
(更新)如果你不能SCSL下获得JDK的代码,然后在这里是一个迂回的方式来获得的JDialog的保持,那么你可以做任何你想要的,包括拆除取消按钮:
progressMonitor = new ProgressMonitor(ProgressMonitorDemo.this,
"Running a Long Task",
"", 0, 100);
progressMonitor.setMillisToDecideToPopup(0);
progressMonitor.setMillisToPopup(0);
progressMonitor.setProgress(0);
JDialog dialog = (JDialog)progressMonitor.getAccessibleContext().getAccessibleParent();
JOptionPane pane = (JOptionPane)dialog.getContentPane().getComponent(0);
pane.setOptions(new Object[]{});
这是丑陋的,它完全依赖于ProgressMonitor的实现。所以你应该检查ClassCastException和null。
为什么你需要设置两个时间段为0?否则,该对话框不会在setProgress中创建。
我知道这个帖子是有点老了,但对谷歌第一个答案,所以我会后我的答案...
我没有解决方案禁用取消按钮,但我使用这个技巧:单击取消时重新显示进度监视器。
public void propertyChange (PropertyChangeEvent event) {
if (event.getPropertyName().equals("progress")) {
int progress = ((Integer) event.getNewValue()).intValue();
lastProgress = progress;
progressMonitor.setProgress(progress);
}
if (progressMonitor.isCanceled()) {
progressMonitor = new ProgressMonitor(panel, "", "", 0, 100);
progressMonitor.setMillisToDecideToPopup(0);
progressMonitor.setMillisToPopup(0);
progressMonitor.setProgress(lastProgress);
}
}
当然,你必须记住最后的进度值。
这个解决方案有点不舒服,但它适用于我。
伊夫
是的,这是可能的:http://stackoverflow.com/questions/2778045/how-to-omit-the-cancel-button-in-java-progressmonitor – 2014-11-04 13:29:48