正如其他人所建议的,最好的方法是使用
SwingWorker。
SwingWorker属性是可监听的,并且总是在EDT中调用监听器,因此,您可以执行以下 *** 作:
public class ArchivingWorker extends SwingWorker<Void, Void> { JProgressBar progressBar = null; // Other members here... ... public ArchivingWorker(...) { // Any specific initialization here (in EDT) addPropertyChangeListener(new PropertyChangeListener() { @Override void propertyChange(PropertyChangeEvent e) { if ( "state".equals(e.getPropertyName()) && e.getNewValue() == Statevalue.STARTED) { // Background thread has just started, show a progress dialog here progressBar = new JProgressBar(); ... } else if ("progress".equals(e.getPropertyName())) { // Update progress bar here with e.getNewValue() ... } } }); } @Override protected Void doInBackground() { // Archiving process here and update progress from time to time setProgress(progress); return null; } @Override protected void done() { // Ensure that archiving process worked correctly (no exception) try { get(); } catch (Exception e) { // Handle exception (user feedback or whatever) } finally { // Close progress dialog ... } }}
然后,您可以
ArchivingWorker根据需要使用:
ArchivngWorker worker = new ArchivingWorker(...);worker.execute();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)