내 JProgressBar
설정 했으므로 원하는대로 작동하지 않습니다. 따라서 프로그램을 실행할 때마다 즉시 0에서 100으로 바뀝니다. 내가 ProgressMonitor
, 작업을 사용하여 시도하고 SwingWorker
시도했지만 아무것도 노력했다.JProgressBar가 제대로 작동하지 않습니다.
int max = 10;
for (int i = 0; i <= max; i++) {
final int progress = (int)Math.round(
100.0 * ((double)i/(double)max)
);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
jProgressBar2.setValue(progress);
}
});
}
@MadProgrammer 여기 스윙 노동자를 만들고 문서에 각각의 이름을 작성하고 진행 표시 줄을 업데이트하려고 시도한 것입니다 :
여기 내 프로그램입니다. 이 프로그램은 약 86 %에 달하며 멈추고 결코 완성 된 문서를 만들지 않습니다. 프로그램이 빈 문서를 만듭니다. 여기
public class GreenWorker extends SwingWorker<Object, Object> {
@Override
protected Object doInBackground() throws Exception {
int max = greenList.size();
XWPFParagraph tmpParagraph;
XWPFRun tmpRun;
FileInputStream file =
new FileInputStream(location + "GreenBandList.docx");
gDocument = new XWPFDocument(OPCPackage.open(file));
for (int i = 0; i < max; i++) {
tmpParagraph = gDocument.getParagraphs().get(0);
tmpRun = tmpParagraph.createRun();
if (greenList.get(i).length() == 1) {
tmpRun.setBold(true);
tmpRun.setText(greenList.get(i));
tmpRun.setBold(false);
} else {
tmpRun.setText(greenList.get(i));//Write the names to the Word Doc
}
int progress = Math.round(((float) i/max) * 100f);
setProgress(progress);
}
return null;
}
}
그리고
그것을 시작하고 내 재산 변경 이벤트가있는 버튼에 대한 코드입니다 : 여기에 먼저 두 가지 방법 내가 만든 SwingWorker의 객체되어 있습니다.private void GenerateGreenList() throws IOException, InterruptedException {
//Need to fix the bug that removes the Letter Header in Yellow Band list
//********************************************************************\\
//Delete the old list and make a new one
File templateFile = new File(location + "\\backup\\GreenTemplate.docx");
FileUtils.deleteQuietly(new File(location + "GreenBandList.docx"));
FileUtils.copyFile(templateFile, new File(location +
"GreenBandList.docx"));
//Get the New Entries
String[] entries = jTextPane3.getText().split("\n");
for (String s : entries) {
if (s != null) {
greenList.add(s);
}
}
//Resort the list
Collections.sort(greenList);
//Write the names to the document
GreenWorker worker = new GreenWorker();
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
jProgressBar2.setValue((Integer) evt.getNewValue());
}
}
});
worker.execute();
if (worker.isDone()) {
try {
gDocument.write(new FileOutputStream(new File(location + "GreenBandList.docx")));
////////////////////////////////////////////////////////////
} catch (IOException ex) {
Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, "Green Band List Created!");
jProgressBar2.setValue(0);
}
}
나는 다른 게시물 중 하나에서 프로퍼티 변경 리스너를 사용하지만 난 정말 당신이 쓴 사람이 무엇을하거나 일반적으로 무엇을 이해하지?
내 멍청한 짓이야.
Brent