-3

I don't understand why the progress bar does not move in the following code:

JDialog dlgProgress = new JDialog(InterfaceYadis.frameInterface, "Veuillez patienter...", true);
JLabel lblStatus = new JLabel("Test");

JProgressBar pbProgress = new JProgressBar(0, 100);
pbProgress.setIndeterminate(true);

dlgProgress.add(BorderLayout.NORTH, lblStatus);
dlgProgress.add(BorderLayout.CENTER, pbProgress);
dlgProgress.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//dlgProgress.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlgProgress.setSize(300, 90);
dlgProgress.setLocationRelativeTo(null);


SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>() {
    @Override
    protected Void doInBackground() throws Exception {
        Thread.sleep(2500);
        return null;
    }

    @Override
    protected void done() {
        dlgProgress.dispose();
    }
};

sw.execute();
dlgProgress.setVisible(true);

The JDialog should be executed on UI thread and the progress bar should move from left to right ? Thanks.

EDIT:

The problem is not on the code, before, i have created a custom JProgressBar and i have modified the repaint interval and cycle time defaults... Custom Jprogress was setting :

UIManager.put("ProgressBar.repaintInterval", new Integer(50));
UIManager.put("ProgressBar.cycleTime", new Integer(100));

The default setting is :

INTERVAL = UIManager.getInt("ProgressBar.repaintInterval"); // = 50
CYCLETIME = UIManager.getInt("ProgressBar.cycleTime"); // = 3000

For me, I have two solutions:

1 / I defined the cycle and the interval each time to be sure to have the right values

2 / I use the same everywhere progressbar

I think the solution 2 is the best, but I like to know if the solution one can be a good solution too.

  • The JProgressBar is set to be indeterminate -- what do you think that this does? Please have a look at the tutorial and API first. – Hovercraft Full Of Eels Oct 01 '16 at 00:03
  • 1
    `The JDialog should be executed on UI thread` - then post a proper [SSCCE](http://sscce.org/) that shows how you do this. `and the progress bar should move from left to right ?` - code works fine for me using JDK8 on Windows 7 – camickr Oct 01 '16 at 00:06
  • Normally JProgressBar should not be static. I want the patient time user of an operation (indeterminate time) – David Silvain Oct 01 '16 at 00:56
  • @DavidSilvain: who mentioned anything about "static"? – Hovercraft Full Of Eels Oct 01 '16 at 00:58
  • camickr, the code work and the progress bar is animated? – David Silvain Oct 01 '16 at 00:59
  • Hovercraft, i'm sorry, when i say static, i mean "no graphic animation" – David Silvain Oct 01 '16 at 01:01
  • 1
    David: what is your question? Why are you posting this here? Is your code not working? Are you having a confusion? Again, please post a [mcve] as I"ve posted below. Or am I wasting my time trying to help you? – Hovercraft Full Of Eels Oct 01 '16 at 01:02
  • Ok, my problem : JProgressBar does not move and it seems that the program no longer responds. (with this code). I'm sorry, I try to do a complete example tomorrow – David Silvain Oct 01 '16 at 01:09
  • Please edit this question when you do so (don't ask a new one), and then notify us by commenting to us, placing an `@` before our names, such as `@hovercraftfullofeels`. – Hovercraft Full Of Eels Oct 01 '16 at 01:10
  • When I test a new project, it works (progress bar move)... I have certainly block the main thread in my project, I look at it tomorrow and I will publish my question. – David Silvain Oct 01 '16 at 01:28
  • @DavidSilvain, `When I test a new project, it works (progress bar move)...` - and that is why you were asked to post a `SSCCE` over an hour ago. The problem is code you don't post and we can't guess why you are blocking the EDT. – camickr Oct 01 '16 at 01:55

1 Answers1

1
  1. You've set the JProgressBar to be indeterminate, so you shouldn't expect left to right changes to it. Rather per the tutorial and the API, the bar will move back and forth
  2. And even if you don't do this, you never set its value, so it will never change.

If you want steady left to right movement with progress:

  1. Don't set the progress bar to be indeterminate. Leave it be
  2. Set it's value as your worker progresses. This is often done using a PropertyChangeListener added to the worker and then within the worker setting the progress property, and within the listener, listening to it.

For example of both:

import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class ProgressBarEg extends JPanel {
    public ProgressBarEg() {
        add(new JButton(new ShowDialogAction("Show Indeterminant Dialog", KeyEvent.VK_I, true)));
        add(new JButton(new ShowDialogAction("Show Determinant Dialog", KeyEvent.VK_D, false)));
        setPreferredSize(new Dimension(500, 350));
    }

    private class MyWorker extends SwingWorker<Void, Void> {

        private static final int DELTA_PROGRESS = 1;
        private static final long SLEEP_TIME = 40;

        @Override
        protected Void doInBackground() throws Exception {
            int progress = getProgress();
            while (progress < 100) {
                progress += DELTA_PROGRESS;
                progress = Math.min(progress, 100);
                setProgress(progress);
                Thread.sleep(SLEEP_TIME);
            }
            return null;
        }
    }

    private class ShowDialogAction extends AbstractAction {
        JDialog dlgProgress;
        private boolean indeterminant;
        JProgressBar pbProgress = new JProgressBar(0, 100);

        public ShowDialogAction(String name, int mnemonic, boolean indeterminant) {
            super(name);
            putValue(MNEMONIC_KEY, mnemonic);
            this.indeterminant = indeterminant;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (dlgProgress == null) {
                Window window = SwingUtilities.getWindowAncestor(ProgressBarEg.this);
                dlgProgress = new JDialog(window, "Veuillez patienter...", ModalityType.APPLICATION_MODAL);
                JLabel lblStatus = new JLabel("Test");

                if (indeterminant) {
                    pbProgress.setIndeterminate(true);
                } else {
                    pbProgress.setStringPainted(true);
                }

                dlgProgress.add(BorderLayout.NORTH, lblStatus);
                dlgProgress.add(BorderLayout.CENTER, pbProgress);
                dlgProgress.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                //dlgProgress.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                dlgProgress.setPreferredSize(new Dimension(300, 90));
                dlgProgress.setLocationRelativeTo(null);
                dlgProgress.pack();
            }

            SwingWorker<Void, Void> sw = new MyWorker();
            sw.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equalsIgnoreCase("progress")) {
                        pbProgress.setValue((Integer) evt.getNewValue()); 
                    } else if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                        dlgProgress.dispose();
                    } 
                }
            });

            sw.execute();
            dlgProgress.setVisible(true);

        }
    }

    private static void createAndShowGui() {
        ProgressBarEg mainPanel = new ProgressBarEg();

        JFrame frame = new JFrame("ProgressBarEg");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • The point of indeterminate is you don't know how long the task will take so the bar just goes back and forth until the progress bar is closed. – camickr Oct 01 '16 at 00:10
  • @camickr: I know that of course. But he seems to assume that it means the bar will move progressively left to right. – Hovercraft Full Of Eels Oct 01 '16 at 00:10
  • Thanks for the answer. I do not necessarily want the bar moves from left to right, just want the bar moves. The JprogressBar is there to make the user wait, if the bar does not move, the user believes that software no longer works ... – David Silvain Oct 01 '16 at 00:54
  • 1
    @DavidSilvain: the problem with your question is you don't show why your current code is not working, suggesting that the problem lies in code not shown. If you still need help, you must create and post a [mcve] similar to what I've posted above, code that is not in a link, that is small, and that compiles, runs, and demonstrates your problem. – Hovercraft Full Of Eels Oct 01 '16 at 00:56