进度条冻结在窗口8上

时间:2014-05-13 09:23:16

标签: java swing swingworker

我正在使用我正在加载对话框的框架。单例对话框包含一个进度条。 我创造了自己的摇摆工作者。我正在控制swing worker的构造方法(其功能类似于doInBackground方法)。 我试图从外部课堂打电话给hidedialog。对话框有时会卡住。我该如何解决呢。它在win7上工作正常但在win8上失败。

  public class LoadingProgressDialogSingleton extends com.manu.scpoweb.common.ds.client.swing.dialog.Dialog {
    protected static LoadingProgressDialogSingleton oneAndOnlyProgressDialog = null;
    JProgressBar progressBar = null;
    DFULoadStatusBean dfuLoadStatusBean = null;
    double progress;
    int progressComplete = 0;
    BusinessObjectUserContext uc = null;
    CopyProgressBarWorker copyProgressBarWorker = null;
    ProgressBarWorker progressBarWorker = null;
    public static synchronized LoadingProgressDialogSingleton getProgressDialog(IApplicationContext iApplicationContext, ClientDataManager dataManager){
        if (oneAndOnlyProgressDialog == null){
            oneAndOnlyProgressDialog = new LoadingProgressDialogSingleton(iApplicationContext.getParentFrame(),
                    iApplicationContext.getResource(APMConstants.RESOURCE_BUNDLE, "ProgressBar.Loading"), Boolean.TRUE);

            oneAndOnlyProgressDialog.progressBar = new JProgressBar(0, 100);
            oneAndOnlyProgressDialog.progressBar.setValue(0);
            oneAndOnlyProgressDialog.progressBar.setPreferredSize(new Dimension(175, 20));
            oneAndOnlyProgressDialog.progressBar.setStringPainted(true);
            oneAndOnlyProgressDialog.progressBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

            JLabel label = new JLabel(iApplicationContext.getResource(APMConstants.RESOURCE_BUNDLE, "ProgressBar.Reloading"));

            JPanel center_panel = new JPanel();
            center_panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.insets = new Insets(2, 6, 2, 6);
            center_panel.add(label, gbc);
            gbc.gridy = 1;
            center_panel.add(oneAndOnlyProgressDialog.progressBar, gbc);
            gbc.gridy = 2;
            center_panel.add(Box.createVerticalGlue(), gbc);

            oneAndOnlyProgressDialog.getContentPane().add(center_panel, BorderLayout.CENTER);
            oneAndOnlyProgressDialog.setLocationRelativeTo(null); // center on screen
            oneAndOnlyProgressDialog.setPreferredSize(new Dimension(350, 107));
            oneAndOnlyProgressDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

            oneAndOnlyProgressDialog.uc = new BusinessObjectUserContext(iApplicationContext.getUser().getUserName(),
                               iApplicationContext.getUserLocale());
            oneAndOnlyProgressDialog.uc.setAttributes(dataManager.getUserContext().getUserPreferences());
        }
        return oneAndOnlyProgressDialog;

    }
    public void showDialog(java.util.List<AttributeUUID> promoOfferKeysOnly){

        final Thread showDialogThread = new Thread(new Runnable() {
                public void run() {
                    toFront();
                    show();
                }
            });
        showDialogThread.start();

        if (promoOfferKeysOnly != null && promoOfferKeysOnly.size() > 0 ) {
           progressBarWorker = new ProgressBarWorker();
           BusinessObjectUserContext uc = new BusinessObjectUserContext(oneAndOnlyProgressDialog.uc.getUserName(),
                   oneAndOnlyProgressDialog.uc.getLocale());
           uc.setAttributes(oneAndOnlyProgressDialog.uc.getUserPreferences());
           progressBarWorker.setBusinessObjectUserContext(uc);
           progressBarWorker.setDataKey(promoOfferKeysOnly.get(0));
           progressBarWorker.start();
        }
    }

    public static synchronized void hideDialog(){
        if (oneAndOnlyProgressDialog != null){
            ProgressBarWorker worker = oneAndOnlyProgressDialog.progressBarWorker;
            CopyProgressBarWorker copyWorker = oneAndOnlyProgressDialog.copyProgressBarWorker;
            oneAndOnlyProgressDialog.dispose();

            if(oneAndOnlyProgressDialog.isShowing())
            oneAndOnlyProgressDialog.setVisible(false);
            oneAndOnlyProgressDialog = null;
            if (worker!=null){
                worker.setProgressComplete();
            }
            if (copyWorker!=null){
                copyWorker.setCopyCompleted();
            }
        }
    }
    protected LoadingProgressDialogSingleton(Frame iParent, String iTitle, boolean iModal) {
        super(iParent, iTitle, iModal);
    }
    class ProgressBarWorker extends SwingWorker {
        BusinessObjectUserContext uc = null;
        AttributeUUID dataKey = null;

        public AttributeUUID getDataKey() {
            return dataKey;
        }

        public void setDataKey(AttributeUUID dataKey) {
            this.dataKey = dataKey;
        }
        public void setProgressComplete(){
            progressComplete = 100;
        }
        public Object construct() {
            progressComplete = 0;
            EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
            if (queue instanceof ManuEventQueue) {
                try {
                    JAASUtilities.runAs(((ManuEventQueue) Toolkit.getDefaultToolkit().getSystemEventQueue()).getSubject(),
                            new PrivilegedExceptionAction() {
                                public Object run() throws Exception {
                                    long totalDFUsCnt = 0;
                                    long basePriceLoadedCnt = 0;
                                    long baseCostLoadedCnt = 0;
                                    long udasLoadedCnt = 0;
                                    long histLoadedCnt = 0;
                                    long fcstLoadedCnt = 0;
                                    progressBar.setValue(5);
                                    //Initialize progress property.
                                    while (progressComplete < 5) {
                                        if (Thread.currentThread().isInterrupted())
                                            break;
                                        if (dataKey != null) {
                                            dfuLoadStatusBean = APMController.getDFULoadStatus(dataKey, uc);
                                            if (dfuLoadStatusBean == null || !dfuLoadStatusBean.isStrated()) {
                                                Thread.currentThread().sleep(5000);
                                                continue;
                                            } else {
                                                if (dfuLoadStatusBean.getNumberOfDFUsInOffer() != null && totalDFUsCnt != dfuLoadStatusBean.getNumberOfDFUsInOffer().getSecond()) {
                                                    totalDFUsCnt = dfuLoadStatusBean.getNumberOfDFUsInOffer().getSecond();
                                                    progressBar.setValue(10);
                                                }
                                                if (dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair() != null && (baseCostLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair().getFirst()) {
                                                        baseCostLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        baseCostLoadedCnt = dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair() != null && (basePriceLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair().getFirst()) {
                                                        basePriceLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        basePriceLoadedCnt = dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusUDAStatusAndCountPair() != null && (udasLoadedCnt != totalDFUsCnt)) {`enter code here
                                                    if (dfuLoadStatusBean.getDfusUDAStatusAndCountPair().getFirst()) {
                                                        udasLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        udasLoadedCnt = dfuLoadStatusBean.getDfusUDAStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusHISTStatusAndCountPair() != null && (histLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfusHISTStatusAndCountPair().getFirst()) {
                                                        histLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;
                                                    } else {
                                                        histLoadedCnt = dfuLoadStatusBean.getDfusHISTStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusFCSTStatusAndCountPair() != null && (fcstLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfusFCSTStatusAndCountPair().getFirst()) {
                                                        fcstLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;
                                                    } else {
                                                        fcstLoadedCnt = dfuLoadStatusBean.getDfusFCSTStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (totalDFUsCnt != 0.0)
                                                    progress = (((baseCostLoadedCnt / totalDFUsCnt * 80) + (basePriceLoadedCnt / totalDFUsCnt * 80) +
                                                            (udasLoadedCnt / totalDFUsCnt * 80) + (histLoadedCnt / totalDFUsCnt * 80) +
                                                            (fcstLoadedCnt / totalDFUsCnt * 80)) / 500d);

                                                if (progress != 0.0) {
                                                    progressBar.setValue((int) (progress * 100));
                                                    Thread.currentThread().sleep(500);
                                                }
                                            }
                                        }
                                    }
                                    return null;
                                }
                            });
                } catch (PrivilegedActionException e) {
                    e.printStackTrace();
                }
            }
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }
        public void finished() {
          }

        public void setBusinessObjectUserContext(BusinessObjectUserContext iUc) {
            uc = iUc;
        }
    }
   }
}

1 个答案:

答案 0 :(得分:0)

根据你的意思&#34;卡住&#34;,我有代码,其中进度似乎停止,因为相关的计时器已被垃圾收集。

换句话说,我不得不将timer变量移动到一个保持其实例的对象。

在我移动变量之前,没有运行时错误,它会停止工作。