我將SwingWorker命名爲Worker;Swingworkers無法獲取GUI變量
public class Worker extends SwingWorker<Void, Void> {
private MainProgramWindow mpw;
public Worker(MainProgramWindow mpw) {
this.mpw = mpw;
}
public String getStartDate(){
String inputStringDate = mpw.startDateBox.getText();
SimpleDateFormat inputFormat = new SimpleDateFormat("dd.MM.yyyy");
Date inputDate = null;
try {
inputDate = inputFormat.parse(inputStringDate);
} catch (ParseException ex) {
Logger.getLogger(MainProgramWindow.class.getName()).log(Level.SEVERE, null, ex);
}
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00.000");
String outputStringDate = outputFormat.format(inputDate);
return outputStringDate;
}
public String getEndDate(){
String inputStringDate = mpw.endDateBox.getText();
SimpleDateFormat inputFormat = new SimpleDateFormat("dd.MM.yyyy");
Date inputDate = null;
try {
inputDate = inputFormat.parse(inputStringDate);
} catch (ParseException ex) {
Logger.getLogger(MainProgramWindow.class.getName()).log(Level.SEVERE, null, ex);
}
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59.999");
String outputStringDate = outputFormat.format(inputDate);
return outputStringDate;
}
@Override
protected Void doInBackground() throws Exception {
//here you make heavy task this is running in another thread not in EDT
int i = 50;
setProgress(i);
//Rest of code.
ResultSet rs1;
Statement stmt;
String query1 = "select date,id,dur from exampletable\n" +
"where adetdate between '"+getStartDate()+"' and '"+getEndDate()+"'";
rs1 = stmt.executeQuery(query1);
//Rest of Code
while(i <= 100){
setProgress(i++);
Thread.sleep(5); // random magic number
}
return null;
}
}
工人成功運行並在指定位置創建excel文件。
我的問題是;當我用StartDate Chooser啓動程序GUI時 - EndDate Chooser - StartJob Buton。當布頓點擊一些進度條調用和進度條執行這個工人和進度條開始聆聽。
但工作人員始終使用第一個GUI顯示startdate和enddate的值。我在Worker中定義了兩個方法來獲取實際的startdate和enddate值並在String query1中使用它們。
爲什麼query1成功調用但不能識別的方法getStartDate()和getEndDate()我不明白。任何想法 ?
編輯: 另外我有PbarNEW類進度條;
package AgentStatGenerator;
//Imports here
public class PbarNEW extends JPanel {
JProgressBar pbar;
public PbarNEW() {
// initialize Progress Bar
pbar = new JProgressBar();
// add to JPanel
add(pbar);
SwingWorker myWorker = new Worker();
myWorker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(final PropertyChangeEvent event) {
switch (event.getPropertyName()) {
case "progress":
pbar.setIndeterminate(false);
pbar.setValue((Integer) event.getNewValue());
break;
}
}
});
myWorker.execute();
}
public static void main(String args[]) {
final PbarNEW it = new PbarNEW();
JFrame frame = new JFrame("Progress Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(it);
frame.pack();
frame.setVisible(true);
}
}
當我點擊MainProgramWindow中的Buton(主GUI)時,出現progresbar並開始監聽Worker。但是SwingWorker myWorker = new Worker();在Worker中創建構造函數後不工作。
private void createExcelButonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String[] arguments = new String[] {"123"};
PbarNEW.main(arguments);
}
感謝您的建議。我編輯我的代碼。添加一些實例並在doinbackround中移動Query blokc。但還是有同樣的問題發生。我選擇02.12.2014和02.12.2014,但是查詢在02.12.2014和02.15.2014執行(這些值在程序啓動時加載) – Lacrymae