如何在进度条达到100%后自动执行另一项任务

时间:2018-03-05 11:13:21

标签: java progress-bar

我在项目中创建了一个进度条,在数据库中创建单词时加载。现在这个工作正常,这里有一些代码(注意:并非所有代码都显示为保密)

表格结构:

/**
     * Launch the application.
     */
    public static void main(String[] args) 
    {
        EventQueue.invokeLater(new Runnable() 
        {
            @Override
            public void run() 
            { 
                try
                {
                    Welcome frame = new Welcome();
                    frame.setVisible(true);
                } 
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    //**************************************************
    // CREATE THE FORM                                 *
    //**************************************************
    public CreateWindow() 
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 163);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        //Progress Bar
        JProgressBar progress = new JProgressBar();
        progress.setBounds(5, 41, 424, 17);
        progress.setStringPainted(true);
        contentPane.setLayout(null);

        //Label above the Progress Bar
        JLabel label = new JLabel("Loop progress is: ");
        label.setBounds(5, 5, 424, 14);
        contentPane.add(label);
        contentPane.add(progress);
        setContentPane(contentPane);

        //Will be used for updating the progress bar 
        ProgressWorker worker = new ProgressWorker(progress);

进度条:

    private static class ProgressWorker extends SwingWorker<Void, Integer> 
        { //Swing worker class for updating the progress bar

            private final JProgressBar progress; //declaration for progress bar

            public ProgressWorker(JProgressBar progress)
            { 
                this.progress = progress;

            }

            @Override
            protected Void doInBackground() throws Exception
            {
             //Task performed here
    }
    System.out.println("i = " + recordLoop);
                    final int progr = ((int) ((100L * (recordLoop - firstRecord)) / (lastRecord-firstRecord)));
                    publish(progr);

                }
                return null;
            }

            @Override
            //This is the process of how the progress bar will load 
            protected void process(List<Integer> chunks) 
            {
                progress.setValue(chunks.get(chunks.size() - 1));
                super.process(chunks); 
            }

            @Override
            protected void done() 
            {
                progress.setValue(100); //This is the value when process is complete 
}
}

现在这很好,因为它按预期工作但现在我想在进度条达到100%后自动运行另一个任务。这是问题发生的地方。这是我尝试过的:

protected void done() 
        {
            progress.setValue(100); //This is the value when process is complete  

            if (int progress == 100) {
                //Do task here 
            }
        }
    }   
}

这不是我尝试过的唯一方式。我也尝试在新的签名方法下添加代码,我也尝试将if语句放在doInBackground()方法中。我是新手,对任何愚蠢的错误感到遗憾。有没有办法可以做到这一点?

如果我的想法不正确我在第一个任务之后通过运行任务而没有使用if语句尝试了这个,但问题是我在doInBackground()方法执行的任务中有一个for循环而我不希望第二项任务包含在for循环中

0 个答案:

没有答案