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();
}
}
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);
}
}
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.
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: If i remove pbar calling from my button, my program working succesfully and create excel file with wanted format. My problem is progressbar feeding.
Thanks in advance.
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.
Read more How to Use Progress Bars.
In a simple what you do is to make a swingWorker sublcass and override
doInBackGround()
.Example:
And in your client code where you have the progressBar:
Here you have an example and another one