gpt4 book ai didi

java - jProgressBar 与数据库

转载 作者:行者123 更新时间:2023-12-01 16:08:43 27 4
gpt4 key购买 nike

当我在数据库中写入内容时,如何在 jProgressBar 中显示进度?

谢谢

最佳答案

您通常会使用 SwingWorker从事件调度线程更新 JProgressBar,同时在后台线程上执行数据库写入操作。例如:

// Create progress bar to represent 10 items we wish to write to
// the database (hence min := 0, max := 10);
JProgressBar pBar = new JProgressBar(0, 10);

// TODO: Add progress bar to panel and display.

// Invoke SwingWorker to perform database write operations on
// background thread.
new SwingWorker<Void, Integer>() {
/**
* Called on background worker thread. Performs DB write operations.
*/
public Void doInBackground() {
for (int i=0; i<10; ++i) {
// TODO: Perform DB write operation here.

// Publish progress so far back to Event Dispatch Thread.
publish(i);
}
}

/**
* Called on EDT with intermediate progress result(s).
*/
protected void process(List<Integer> chunks) {
if (!chunks.isEmpty()) {
int progress = chunks.get(chunks.size() - 1);
pBar.setValue(progress);
}
}

/**
* Called on EDT when DB write task has completely finished.
*/
protected void done() {
pBar.setValue(pBar.getMaximum());
// TODO: Hide progress bar, etc.
}
}.execute()

关于java - jProgressBar 与数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2007002/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com