0

I have a long method on a listener and I want to show a progress bar with JProgressBar class

btnEsegui.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {             

            crypt();
        }
    }); 

I want to understand if is correct, where to place the thread

SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // PROGRESS BAR
                    framePBar = new JFrame("");
                    framePBar.setBounds(
                            MainConstants.infoWindowPos_X, MainConstants.infoWindowPos_Y, 
                            MainConstants.infoWindowDim_X, MainConstants.infoWindowDim_Y);

                    pBarPanel = new JPanel();
                    pbar = new JProgressBar();
                    pbar.setMinimum(0);
                    pbar.setMaximum(100);
                    pbar.setIndeterminate(true);
                    pBarPanel.add(pbar);
                    framePBar.getContentPane().add(pBarPanel);
                    framePBar.setVisible(false);
                }
            });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Noomak
  • 371
  • 5
  • 19
  • you would want the event listener to start a new thread that runs `crypt`. – programmerjake May 26 '15 at 14:05
  • 2
    Take a look to [Worker Threads and SwingWorker](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html). It sounds like you have a time consuming task -`crypt()` method call- that should run in a background thread while you show an indeterminate progress bar, so `SwingWorker` is probably the way to go. – dic19 May 26 '15 at 14:05
  • 2
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) It is not clear if the code is using more than one frame, but the fact that the frame shown only holds a progress bar is a warning sign .. – Andrew Thompson May 26 '15 at 15:08

0 Answers0