Gumovvy Steven
Gumovvy Steven

Reputation: 111

How to set up JProgressbar of running aplication

My application run's about 7 second's.

I want to make a progres bar of that but I don't know how.

Could you help me ?

Upvotes: 0

Views: 113

Answers (3)

alexandre1985
alexandre1985

Reputation: 1106

you should use the progress bar as an indeterminated mode.
You can use something like this:

private void dialogWaiting()
    {
        JProgressBar progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);
        JTextArea msgLabel;
        final JDialog dialogWaiting;
        JPanel panel;

        msgLabel = new JTextArea("Please wait...");
        msgLabel.setEditable(false);


        panel = new JPanel(new BorderLayout(5, 5));
        panel.add(msgLabel, BorderLayout.PAGE_START);
        panel.add(progressBar, BorderLayout.CENTER);
        panel.setBorder(BorderFactory.createEmptyBorder(11, 11, 11, 11));

        dialogWaiting = new JDialog(Frame.getFrames()[0], "Doing whatever", true);
        dialogWaiting.getContentPane().add(panel);
        dialogWaiting.setResizable(false);
        dialogWaiting.pack();
        dialogWaiting.setSize(300, dialogWaiting.getHeight());
        dialogWaiting.setLocationRelativeTo(null);
        dialogWaiting.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        dialogWaiting.setAlwaysOnTop(true);     
        msgLabel.setBackground(panel.getBackground());

        SwingWorker worker = new SwingWorker() {

            @Override
            protected void done() {
                // Close the dialog
                dialogWaiting.dispose();
            }

            @Override
            protected Void doInBackground() {
                // Do the long running task here
                // put all the code you want to run as the dialogWaiting is on

                return null;
            }
        };

        worker.execute();
        dialogWaiting.setVisible(true);
    }

Upvotes: 2

WonderWorld
WonderWorld

Reputation: 966

Basicly it's as simple as, lets say the application has 5 methods that are executed after another. You could set the progress of the bar to +20% after each method has finished.

public class App {
    public void method_1(){
        // method code
        progressbar.setValue(20);
    }
     public void method_2(){
        // method code
        progressbar.setValue(40);
    }
    public void method_3(){
        // method code
        progressbar.setValue(60);
    }
    // etc

It could be during a specific Thread or maybe when a thread has finished, you could even reset it to 0 and start again when a new Thread has started. That all upto you.

Upvotes: 1

nom
nom

Reputation: 266

One way to do it (which is a little stupid) is to use the setValue() method various times in your code. Example:

void main(String[]args){
//assuming that you have imported the progressbar class and have instantiated it
//with the variable name "jp"
System.out.println();//simulating some work...
jp.setValue(10);
System.out.println()//simulating some work...
jp.setValue(20);
//you get the idea...

Upvotes: 0

Related Questions