使用后台数据进行激活,在主要外部激活进度条

时间:2015-07-20 05:35:23

标签: multithreading swingworker jprogressbar

我正在尝试激活进度条,显示我在阅读文件时将“for循环”推进并将其写入excel文件。 问题是,在我尝试的每种方式中,直到for循环结束时,条形图才会更新到用户。这样做的正确方法是什么?是否可以在MyPanel类中间激活进度条?

我有下一堂课: 测试器(包含主要),MyPanel,LogFileReader,DefectFileReader。

测试

public class Tester {

    public static void main(String[] args){

        JFrame frame = new JFrame("Analayzer GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(450, 300);
        frame.setLocationRelativeTo(null);
        MyPanel p = new MyPanel();
        frame.add(p);
        frame.setVisible(true);

    }
}

我的面板(仅限相关代码)

public class MyActionListener implements ActionListener  
{
    public void actionPerformed(ActionEvent click) 
    {
        /*...........(code irelevant).......*/

        if(click.getSource() == analayze){
            Boolean valid = isValidToStart();
            if(valid == true){
                try{
                fos = new FileOutputStream("c:/temp/analayze.xlsx");
            if(DFloaded == true){
                try {
                    pbar.repaint();
                    DF = new File[DFLIST.length];
                    dfReader = new DefectFileReader[DFLIST.length];
                    docArray = new Document[DFLIST.length];
                    for(int i = 0 ; i < DFLIST.length ; i++){
                     /******* This is where i want to update my bar according to (i/DFList.length) ********/
                        System.out.println("i'm sending the file down here to parse from file to document");
                        System.out.println(DFLIST[i].getAbsoluteFile());
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        docArray[i] = (Document) db.parse(DFLIST[i]);
                        docArray[i].getDocumentElement().normalize();
                        dfReader[i] = new DefectFileReader(DFLIST[i] , docArray[i]);
                        dfReader[i].getRunsIndex();
                        totalDuration = sumTimes(dfReader[i].inspectionDuration());
                        if(i==0){
                            firstWriteToExcel(dfReader[0], sheet, workbook, fos);
                            writeToExcel(dfReader[0], sheet, workbook, fos);
                        }
                        if(i!=0)
                            writeToExcel(dfReader[i] , sheet ,  workbook , fos );
                        repaint();
                    if(INPloaded == true){

                    }
                    DFLIST[i] = null;
                    dfReader[i].clearDfReader();
                    docArray[i] = null;
                    System.gc();
                   }
                }
                   catch(Exception e ) {
                      System.out.println(e);
                      System.exit(0);
                   }
            }

            row = sheet.createRow(rowIndex);
            totalDuration = sumTimes("00:00:00");
            if(inspectionDuration.getState() == true){
            row.createCell(cellIndex - 1).setCellValue(totalDuration);
            row.getCell(cellIndex - 1).setCellStyle(csForFirstWrite);
            }
            ((Workbook) workbook).write(fos);
            workbook.close();
            JOptionPane.showMessageDialog(null, "Your data has been analyze!\n you can find the result in c:\\temp");
            System.exit(0);

        }catch(Exception e){
            e.printStackTrace();
        }

    }
}

0 个答案:

没有答案