I am working on a program where there are several tasks that take time that users can implement. I would like a progress bar to pop up to inform the user how far the task has gotten. When the user presses a button on the main frame, I want a pop-up to start automatically and I would like the user to not be able to press anything on the main frame till the job is done. The bar should disappear when the task is done and control returned to the main frame.

My problem (so far) is that if I make my JDialog modal, then the user has to press a button on it to get the progress bar to implement, and it won't close automatically. If I don't make the JDialog modal, then the user can press buttons on the main frame before the task is complete. My code is below. Currently the JDialog is non-modal, starts automatically, and disappears when done (which I want), but as mentioned, the user can press the button on the main frame while the task is implementing (which I do not want). Code is below.

// Filename : MainFrame.java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class MainFrame extends JFrame implements ActionListener
{
     JButton task1Button;
     JButton taskCompleteButton;
     ProgressBar pb;
    
     public MainFrame ()
     {
         this.setTitle("Main Frame");
         task1Button = new JButton ("Task 1");
         task1Button.addActionListener(this);
         this.add (task1Button);
         this.setLayout (new FlowLayout ());
         this.setSize (600, 600);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setVisible (true);         
     }
          
     public static void main (String args[])
     {
         new MainFrame ();
     }

     public void actionPerformed(ActionEvent e)
     {
         if (e.getSource() == task1Button)
         {
             ImplementTask1 ();
         }
     }
     
     public void ImplementTask1 ()
     {
         pb = new ProgressBar (this);
     }
     
     public void Task1Complete ()
     {
         pb.setVisible(false);
     }
}
// Filename : ProgressBar.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class ProgressBar extends JDialog implements Runnable, ActionListener
{
    JProgressBar pb;
    int percentComplete;
    JButton startProgressButton;
    MainFrame mf;
    Thread t;
    
    public ProgressBar (MainFrame parent)
    {
        super (parent, false);
        mf = parent;
        this.setTitle("Progress Bar");
        this.setSize (300, 100);
        this.setLayout (new FlowLayout ());
        JPanel panel = new JPanel ();
        panel.setLayout (new FlowLayout ());
        pb = new JProgressBar (0, 100);
        panel.add (pb);
        panel.setBackground (Color.BLUE);
        startProgressButton = new JButton ("Start Bar");
        startProgressButton.addActionListener (this);
        panel.add (startProgressButton);
        this.getContentPane().add (panel);
        this.setVisible (true);
        percentComplete = 0;
        this.pack ();
        StartTask ();
    }
    
    public void StartTask ()
    {
        t = new Thread (this);
        t.start ();        
    }
    
    public void UpdateProgress (int percComplete)
    {
        pb.setValue(percComplete);
        pb.repaint();
    }
    
    public void TaskFinished ()
    {
        mf.Task1Complete();
    }

    public void run()
    {
        for (int i=0; i<=100; i++)
        {            
            UpdateProgress (i);
            try
            {
                Thread.sleep(20);
            }
            catch (InterruptedException ex)
            {
            }
        }
        
        TaskFinished ();
    }

    public void actionPerformed(ActionEvent arg0)
    {
        StartTask ();
    }
}

I set modality in line 19 in ProgressBar.java. In line 36, the function call to StartTask () seems not to do anything if the JDialog is modal and I do not know why.

Try calling startTask() before you set the dialog visible.

You may also want to glance at this article on a modal progress monitor: http://www.javalobby.org/java/forums/t53926.html

Awesome Ezzaral. Changing the location of the function call did the trick. Thanks so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.