如何省略“取消” Java ProgressMonitor中的按钮? [英] How to omit the "Cancel" button in Java ProgressMonitor?
本文介绍了如何省略“取消” Java ProgressMonitor中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的任务是必要的,不应取消,如何让ProgressMonitor不显示取消按钮,所以当它完成时,它会自动关闭面板。
My task is necessary and shouldn't be canceled, how do I ask ProgressMonitor not to display the "Cancel" button, so when it finishes, it will auto close the panel.
Frank
推荐答案
我想也许我可以问它
返回其中的组件并删除
按钮
I was thinking maybe I can ask it to return the components in it and delete the button
使用Swing教程中的ProgressMonitorDemo(由BalusC链接)我做了以下更改:
Using the ProgressMonitorDemo from the Swing tutorial (linked to by BalusC) I made the following changes:
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName() ) {
int progress = (Integer) evt.getNewValue();
progressMonitor.setProgress(progress);
// Added this
AccessibleContext ac = progressMonitor.getAccessibleContext();
JDialog dialog = (JDialog)ac.getAccessibleParent();
java.util.List<JButton> components =
SwingUtils.getDescendantsOfType(JButton.class, dialog, true);
JButton button = components.get(0);
button.setVisible(false);
// end of change
String message =
String.format("Completed %d%%.\n", progress);
progressMonitor.setNote(message);
taskOutput.append(message);
if (progressMonitor.isCanceled() || task.isDone()) {
Toolkit.getDefaultToolkit().beep();
if (progressMonitor.isCanceled()) {
task.cancel(true);
taskOutput.append("Task canceled.\n");
} else {
taskOutput.append("Task completed.\n");
}
startButton.setEnabled(true);
}
}
}
您需要下载 Swing Utils 课程。
代码只能执行一次,否则在对话框关闭时会得到一个NPE。我会让你整理一下:)。
The code should only be executed once, otherwise you get a NPE when the dialog closes. I'll let you tidy that up :).
这篇关于如何省略“取消” Java ProgressMonitor中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文