0

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 ?

Gumovvy Steven
  • 101
  • 2
  • 10
  • Do you have some code to show what you have tried so far? – WonderWorld Mar 21 '15 at 18:19
  • Do you want 3k lines of code ? Everything what i want is to show small window with progress bar before app is execute, I think you don't have to see any code if You know how to do it – Gumovvy Steven Mar 21 '15 at 18:26
  • :) no not really.(@3k lines of code) But to have a progressbar display accurate values i imagine it takes more then an application running "about 7 seconds". You would have to look at the processes going on in the app and accordingly update the progressbar. – WonderWorld Mar 21 '15 at 18:29
  • This isn't a question, but a request. That's why @WonderWorld wanted you to post some code. If you're not able to eliminate the problem and provide few lines of code (definitely not few thousands) for us to see how your project is designed, it's your problem, not ours. – tzima Mar 21 '15 at 19:13
  • possible duplicate of [Java progressbar feeding](http://stackoverflow.com/questions/21779825/java-progressbar-feeding) – tzima Mar 21 '15 at 19:21

3 Answers3

2

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);
    }
alexandre1985
  • 1,056
  • 3
  • 13
  • 31
1

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.

WonderWorld
  • 956
  • 1
  • 8
  • 18
0

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...
nom
  • 256
  • 3
  • 16