0

I need to show the progress of a task in my project in these case the progress of the method ProcBaseCentral(jfSelectorArchivo.getSelectedFile()), in the progressbar. This method calls a sql query that takes a indefinitly time to finish.The indeterminate mode works for me, but i need to see some progress because is important to show the progress.

I managed to make good progress in the code, I can show the percentage but still have not found a way to measure progress. The progress bar doesn't finish at the same time of the method or function, I know that my code is wrong, I just don't find a way to measure the progress. Maybe someone can help me with an algorithm.

The next code is what i have so far.

private void bProcesar3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        final int N = 100;        
        bProcesar3.setEnabled(false);
        class MyWorker extends SwingWorker<Double, Double> {

            @Override
            public Double doInBackground() throws InterruptedException {
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

                Thread t = new Thread(new Runnable() {
                    public void run() {
                        ProcesarSOC(jfSelectorArchivo.getSelectedFile());
                    }
                });
                int cntr = 1; //
                int cont = 0;
                double d1;
                double d2;
                double d3;
                t.start();
                while (t.isAlive()) {
                    d1 = N;
                    d2 = cont++;
                    d3 = d2 / d1;
                    d3 = d3 * 100;
                    if (d3 >= cntr) {
                        cntr++;
                        setProgress(cntr);
                        publish(Double.valueOf(cntr));                        
                    }
                }
                return Double.valueOf(0);
            }

            @Override
            protected void process(List<Double> chunks) {
                for (double d : chunks) {
                    System.out.println(": " + d);
                }
            }

            @Override
            public void done() {
                Toolkit.getDefaultToolkit().beep();
                setCursor(null);
            }
        }
        MyWorker task = new MyWorker();
        task.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent e) {
                if ("progress".equals(e.getPropertyName())) {
                    jProgressBar1.setIndeterminate(false);
                    jProgressBar1.setValue((Integer) e.getNewValue());
                    System.out.println("**: " + e.getNewValue());
                }
            }
        });
        task.execute();
    }

Thanks for your time.

jdrageme01
  • 69
  • 3
  • 9
  • 1
    if it's an indefinite time how can you show progress? If you have a percentage then showing a progress bar to represent it shouldn't be difficult. By the definition of indefinite, there's no real way you can show progress. You may be able to do trick them. Make it take 5 seconds to fill half the bar, then another 5 seconds to fill the next 25%. Then another 5 seconds to fill the next 12.5%. This way the bar is always increasing, but gets slower and slower. When it finishes, just jump the bar to the end. – Cruncher Nov 15 '13 at 16:46
  • @Cruncher Where I can add what you suggest. – jdrageme01 Nov 15 '13 at 16:58
  • It would be a little tricky, and probably take a fair bit of code. I don't even know how well it would really work – Cruncher Nov 15 '13 at 17:03
  • To complement @Cruncher's comment, you can average the operation time (including worst/best case) and design the progress strategy based on that time – higuaro Nov 15 '13 at 18:29

0 Answers0