Search code examples
javaswingprogress-bar

JProgressBar Dialog


I'm trying to create a pop up dialog progress bar preferably an Indeterminate but that's not too important. I have been looking through "Oracle's ProgressBar Tutorials" and Google searching but not such luck in getting it to work. I'm pasted my code below of my Action Listener and the dialog will not pop up. Any suggestions?

Sorry this is my first post on this site. But how it works is that When I press the create button, it goes out and grab some information from different servers and directories and creates a file for me. That is what the new Project is. Features is a Enumeration I made that are set with the text in the JTextBox when the Create Button. The problem is that this process takes time to process, so I want the a progress bar to show that its processing

private class CreateButton implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent arg0) { 
        class Task extends SwingWorker<Void, Void>
        {
            @Override
            protected Void doInBackground()
            {                   
                //Set Variables
                for(Feature f : Feature.values())
                {
                    if(f.getComp() != null)
                    {
                        f.getVariable().setVariable(((JTextField) f.getComp()).getText());
                    }
                }

                new Project(jobs.getSelectedValue().split("-")[0].trim(), 
                            jobs.getSelectedValue().split("-")[1].trim(),
                            features);
                return null;
            }
        }
        
        ProgressMonitor pm = new ProgressMonitor(display, "Testing...", "", 0, 100);
        pm.setProgress(0);
        Task task = new Task();
        task.execute();
    }
    
}   

Solution

  • This comes from the Oracle javadoc for ProgressMonitor:

    Initially, there is no ProgressDialog. After the first millisToDecideToPopup milliseconds (default 500) the progress monitor will predict how long the operation will take. If it is longer than millisToPopup (default 2000, 2 seconds) a ProgressDialog will be popped up.

    Note that it doesn't pop up until at least 1/2 second after you create it. Even then, it only pops up if the process is expected to take over 2 seconds.

    This is all based on your repeated calls to setProgress(int) and the time between the progression of values across the range you gave it.

    I suspect the conditions that cause the dialog to pop up are not being met. Or, perhaps, your program exits before that amount of time goes by.