1

I have main file that use ProgressBar. In main file I call ProgressBar as

ProgressBar pbFrame = new ProgressBar();
pbFrame.setVisible(true);

I call ProgressBar after calling executable file "calculate.exe" to show that calculate.exe is working now. But ProgressBar opens when "calculate.exe" finished. How to make parallel executing of "calculate.exe" and ProgressBar? I hear about SwingWorker but I absolutely don't understand how to use it in my application.

My ProgressBar file:

public class ProgressBar extends JFrame {

static private int BOR = 10;
private String filename;

public ProgressBar() {
super("Calculating progress");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(BOR, BOR, BOR, BOR));
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    panel.add(new JLabel("Calculating..."));

    panel.add(Box.createVerticalGlue());

    JProgressBar progressBar1 = new JProgressBar();
    progressBar1.setIndeterminate(true);        
    panel.add(progressBar1);

    panel.add(Box.createVerticalGlue());

    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));

    buttonsPanel.add(Box.createVerticalGlue());

    JButton quitButton = new JButton("OK!");
    quitButton.setHorizontalAlignment(JButton.CENTER);
    quitButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent event) {
           dispose();
      }
   });

   panel.add(quitButton);

   getContentPane().setLayout(new BorderLayout());
   getContentPane().add(panel, BorderLayout.CENTER);
   setPreferredSize(new Dimension(200, 110));
   setLocationRelativeTo(null);
   pack();
}

}

thanks in advance!

Denis
  • 503
  • 8
  • 32
  • _I absolutely don't understand how to use it_ well, I have a hard time understanding what can possibly un-understandable with the example in the api doc ... start there (and or the tutorial referenced in the swing tag wiki), try to adjust it to your context and come back with a SSCCE that demonstrates where exactly you are stuck – kleopatra Oct 23 '13 at 10:10

2 Answers2

1

You are right, SwingWorker is the way to go. It's kind of hard to give the full code with the information given in your question but it works in this scheme:

SwingWorker worker = new SwingWorker<MyReturnType, Void>() {

    @Override
    public MyReturnType doInBackground() {
        // do your calculation and return the result. Change MyReturnType to whatever you need
    }

    @Override
    public void done() {
        // do stuff you want to do after calculation is done
    }
};

See the official tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/simple.html

André Stannek
  • 7,773
  • 31
  • 52
  • thanks for answer, it works too! but there small problem - progress bar frame opens, but string with percents appear in 1-2 seconds. Why? Is the reason in work of "calculate.exe"? – Denis Oct 25 '13 at 14:02
  • You need to set the progress yourself. I don't know about calling exe files from Java. I guess it's a little hard to get anything else out of it than 0% and 100% progress. – André Stannek Oct 25 '13 at 19:59
1

You can use Executors for background processes. In this case your progressBar will be working in EDT and your calculate.exe in another thread. Try this code:

ProgressBar pbFrame = new ProgressBar();
pbFrame.setVisible(true);       
Executors.newSingleThreadExecutor().execute(new Runnable() {

            @Override
            public void run() {
                // run background process

            }
        });
alex2410
  • 10,904
  • 3
  • 25
  • 41