简体   繁体   中英

Why doesn't the progress bar progress?

Following function is called to move the progress bar but I don't know why it doesn't move till other process just works on.

private void startProgressBar() {
    signInProgressBar.setMinimum(0);
    signInProgressBar.setMaximum(10);
    Runnable r = new Runnable() {
        @Override
        public void run() {
            int p = 1;
            while(!loginCompleted) {
                signInProgressBar.setValue(p);
                //System.out.println(p);
                p++;
                try {Thread.sleep(5000);}catch(Exception exc) {}
            }                    
        }
    };
    new Thread(r,"progress_bar_thread").start();
}

Snippet that calls startProgressBar :

        startProgressBar(); // CALL
        String username = usernameTextField.getText();
        String password = new String(passwordField.getPassword());
        Openfire server = new Openfire();
        boolean isConnected = server.connect(username,password);
        if(isConnected) {
            // Stash the username and password
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);

            // Stop the progress bar
            loginCompleted = true;

            // Display the next window
            UserGUI blab = new UserGUI();
            blab.setVisible(true);
            this.dispose(); // Dispose off the login window
        }

What could be the problem ?

Your code doesn't appear to adjust the progress bar at all:

signInProgressBar.setValue(5);  // <-- in your while loop

This just continually sets the bar's position to 5 , which appears to be about 50% on your scale. Perhaps if you are unsure of the length of your task, you should make your progress bar indeterminate ?

You should also be using the EDT to adjust the progress bar with setValue() . See the Concurrency in Swing tutorial for more details.

All of this stuff:

    Openfire server = new Openfire();
    boolean isConnected = server.connect(username,password);
    if(isConnected) {
        // Stash the username and password
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        // Stop the progress bar
        loginCompleted = true;
        // ....

... should be done on a background thread, especially connecting and interacting with the server, else you'll be tying up the Swing event thread and with it your entire GUI. Also use a SwingWorker for your background thread and just set its value which the JProgressBar can easily follow via a PropertyChangeListener.

while(!loginCompleted) {
            signInProgressBar.setValue(5);
            //System.out.println(p);
            p++;
            try {Thread.sleep(5000);}catch(Exception exc) {}
        } 

You made a small mistake signInProgressBar.setValue(5); should be ignInProgressBar.setValue(p);

The current code when starts will generate a empty bar then immidieatly will set it to 50% , there is no statement that progresses it after login is complete.

You may need repaint and invalidate .

try

signInProgressBar.invalidate();
signInProgressBar.repaint();

and why you set always 5 for signInProgressBar.setValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM