【发布时间】:2011-04-12 19:39:35
【问题描述】:
我有几个 C/C++ 命令行工具,我用 Java.Swing 作为 GUI 进行了包装。命令行工具可能需要几分钟到几小时。进度条似乎是让用户保持理智的好主意。我还认为为进度条包装一个 GUI 可能会很好,而不是仅仅使用系统输出。但是怎么做?
我认为命令行工具可以将百分比写入标准错误,并且我可以以某种方式在 java 中读取它。不完全确定这样做的机制是什么。我也不清楚异步显示(了解了一些关于 invokeLater() )。 Java 新手,也希望得到一般性建议。谢谢。
---更新---
感谢大家的建议。这是生成的代码。
private void redirectSystemStreams() {
OutputStream out_stderr = new OutputStream() {
@Override
public void write(final int b) throws IOException {
update(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
update(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setErr(new PrintStream(out_stderr, true));
}
private void update(final String inputText) {
int value = 20; //parse inputText; make sure your executable calls fflush(stderr) after each fprintf().
jProgressBar.setValue(value);
/* Also one can redirect to a textpane
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//update jTextPane with inputText
}
});
*/
}
【问题讨论】:
标签: java swing progress-bar