问题描述
我有MainProgramWindow GUI。这个GUI有一些变量,一个按钮调用Sql类的createExcel方法并同时启动进度条。
I have MainProgramWindow GUI. This GUI has some variables and one button call createExcel method of Sql class and start progress bar same time.
public class MainProgramWindow extends javax.swing.JFrame {
package AgentStatGenerator;
//Rest of code.
class Sql {
//Rest of code.
public int newvalueforpbar = 0;
SwingProgressBarExampleOLD2 progressbar = new SwingProgressBarExampleOLD2();
public void createExcel() {
try{
//Rest of code.
stmt = con.createStatement();
rs1 = stmt.executeQuery(query1);
while (rs1.next()) {
HSSFRow row2 = sheet.createRow((short) index);
Cell cell100 = row2.createCell((short) 0);
cell100.setCellValue(rs1.getString(1));
cell100.setCellStyle(stylersinfo);
Cell cell101 = row2.createCell((short) 1);
cell101.setCellValue(rs1.getInt(2));
cell101.setCellStyle(stylersthousand);
Cell cell102 = row2.createCell((short) 2);
cell102.setCellValue(rs1.getDouble(3));
cell102.setCellStyle(stylersdouble);
index++;
newvalueforpbar = 50;
progressbar.updateBar(newvalueforpbar);
}
rs2 = stmt.executeQuery(query2);
while (rs2.next()){
HSSFRow row3 = sheet.createRow((short) index);
Cell cell103 = row3.createCell((short) 0);
cell103.setCellValue(rs2.getString(1));
cell103.setCellStyle(stylersinfo);
Cell cell104 = row3.createCell((short) 1);
cell104.setCellValue(rs2.getInt(2));
cell104.setCellStyle(stylersthousand);
Cell cell105 = row3.createCell((short) 2);
cell105.setCellValue(rs2.getDouble(3));
cell105.setCellStyle(stylersdouble);
index++;
newvalueforpbar = 100;
progressbar.updateBar(newvalueforpbar);
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.toString());
}
}
}
}
private void createExcelButonActionPerformed(java.awt.event.ActionEvent evt) {
Sql sql1 = new Sql ();
SwingProgressBarExampleOLD2 progressbar = new SwingProgressBarExampleOLD2();
progressbar.startProgress();
sql1.createExcel();
}
}
我的SwingProgressBarExampleOLD2类
My SwingProgressBarExampleOLD2 class
package AgentStatGenerator;
import java.lang.reflect.Constructor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
/**
*
* @author Lacrymae_Ev
*/
public class SwingProgressBarExampleOLD2 extends JPanel {
JProgressBar pbar;
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
public SwingProgressBarExampleOLD2() {
// initialize Progress Bar
pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
// add to JPanel
add(pbar);
}
public void updateBar(int newValue) {
pbar.setValue(newValue);
}
public void startProgress() {
final SwingProgressBarExampleOLD2 it = new SwingProgressBarExampleOLD2();
JFrame frame = new JFrame("ProgressBar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(it);
frame.pack();
frame.setVisible(true);
}
}
我在MainProgramWindow中按createExcelButon GUI,sql1.createExcel()方法成功执行并开始工作,我的进度条也出现了。但只有jframe的progresbar没有填充bpar并等到sql1.createExcel()完全完成。
I When i press createExcelButon in MainProgramWindow GUI, sql1.createExcel() method succesfully eecuted and begin working also my progressbar appear. But only jframe of progresbar coming not filled with bpar and waiting until sql1.createExcel() fully complete.
当sql1.createExcel()完全完成并创建excel文件时位置,进度条jframe用pbar填充但是没有进展。但我用progressbar.updateBar(newvalueforpbar)提供pbar;在每个while语句结束之后。
When sql1.createExcel() fully complete and create excel file under defined location, progressbar jframe filled with pbar but bar not progressed. But i feed pbar with progressbar.updateBar(newvalueforpbar); after each while statement end.
P.S:如果我从我的按钮中删除pbar调用,我的程序成功运行并创建所需格式的excel文件。我的问题是进度栏。
P.S: If i remove pbar calling from my button, my program working succesfully and create excel file with wanted format. My problem is progressbar feeding.
提前致谢。
推荐答案
您正在同一个线程中执行所有任务课程来实现这一目标。这里有一个使用progressBar的好例子。 。
You are executing all task in the same thread The Event Dispatch Thread. A good way to solve that is executing business code in a separate thread to don't block the gui and you can update your gui without waiting your business code finish. Swing provides SwingWorker class to achieve that. Here you have a good example with a progressBar. Swing Worker Example.
阅读更多。
简单来说,你做的是做一个swingWorker sublcass并覆盖 doInBackGround()
。
In a simple what you do is to make a swingWorker sublcass and override doInBackGround()
.
示例:
public class Worker extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
//here you make heavy task this is running in another thread not in EDT
int i = 0;
setProgress(i);
// call query 1
while(i < 50){
setProgress(i++);
Thread.sleep(5); // random magic number
}
// then call query 2
while(i <= 100){
setProgress(i++);
Thread.sleep(5); // random magic number
}
return null;
}
}
在您拥有progressBar的客户端代码中:
And in your client code where you have the progressBar:
SwingWorker myWorker = new Worker();
myWorker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(final PropertyChangeEvent event) {
switch (event.getPropertyName()) {
case "progress":
myProgressBar.setIndeterminate(false);
myProgressBar.setValue((Integer) event.getNewValue());
break;
}
}
});
//then to get start you have to use execute()
worker.execute();
这里有和另一个
这篇关于Java进度条喂养的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!