मैंने अपना स्वयं का स्विंगवर्कर उदाहरण बनाया है ताकि यह परिचित हो सके कि यह कैसे काम करता है। जब बटन क्लिक किया मैं एक प्रगति बार दिखाई देते हैं जब तक कार्य किया जाता है मैं बस प्रगति बार को हटाने और संवाद के लिए एक स्ट्रिंग जोड़ना चाहते हैं:मैं अपने स्विंगवर्कर उदाहरण को सही तरीके से कैसे काम करूं?
मुझे क्या करना चाहते कर रहा हूँ निम्नलिखित है।
जब बटन क्लिक किया जाता है, तो प्रगति पट्टी आती है लेकिन कभी नहीं जाती है। (10 सेकंड के बाद प्रगति बार कभी नहीं दूर करता है और लेबल देता है कभी नहीं)
यहाँ एक SSCCE है:
package swingtesting;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class SwingTesting {
/**
* Creates a frame that will hold a simple button to make use of SwingWorker
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame();
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GuiWorker().execute();
}
});
button.setText("Test Me");
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
}
class GuiWorker extends SwingWorker<Integer, Integer> {
/*
* This should just create a frame that will hold a progress bar until the
* work is done. Once done, it should remove the progress bar from the dialog
* and add a label saying the task complete.
*/
private JFrame frame = new JFrame();
private JDialog dialog = new JDialog(frame, "Swingworker test", true);
private JProgressBar progressBar = new JProgressBar();
public GuiWorker() {
progressBar.setString("Waiting on time");
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
dialog.getContentPane().add(progressBar);
dialog.pack();
dialog.setVisible(true);
}
@Override
protected Integer doInBackground() throws Exception {
Thread.sleep(10000);
return 0;
}
@Override
protected void done() {
JLabel label = new JLabel("Task Complete");
dialog.getContentPane().remove(progressBar);
dialog.getContentPane().add(label);
}
}
आप पर एक नज़र ले जा सकते हैं [इस उत्तर] (http://stackoverflow.com/questions/8916721/java-swing-update-label/8917565#8917565) और अपने कोड के साथ तुलना करें। स्विंग ट्यूटोरियल में एक उदाहरण भी शामिल है (लिंक 'जेपी प्रोग्रेसबार' के वर्ग जावाडोक में उपलब्ध है) – Robin