0

I have a loading bar that is not repainting every time I set it's value. Iv'e tried maximizing and minimizing the screen many times but it still shows 0% progress. The code is not finished yet. Any idea? Code:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LoadingTest {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        F f = new F("Loading Bar Test");
    }
}
class F extends JFrame implements Runnable, ActionListener {
    private static final long serialVersionUID = 4684842549708528589L;
    private JProgressBar variableBar = new JProgressBar(0,100);
    private JProgressBar randomGenBar = new JProgressBar(0,100);
    private JProgressBar addBar = new JProgressBar(0,100);
    private JProgressBar totalbar = new JProgressBar(0,100);
    private JButton button;
    public F(String str) {
        setTitle(str);
        setDefaultCloseOperation(3);
        getContentPane().setLayout(new GridLayout(9,1));
        variableBar.setStringPainted(true);
        variableBar.setValue(0);
        button = new JButton("Run Tasks");
        button.addActionListener(this);
        add(button);
        add(new JLabel("Creating variables..."));
        add(variableBar);
        add(new JLabel("Generating Random Values..."));
        add(randomGenBar);
        add(new JLabel("Adding Values..."));
        add(addBar);
        add(new JLabel("Total Progress:"));
        add(totalbar);
        pack();
        setVisible(true);
    }
    public void run() {
        int p = 5;
        int i1;
        variableBar.setValue(p);
        int i2;
        p += 5;
        variableBar.setValue(p);
        int i3;
        p += 5;
        variableBar.setValue(p);
        int i4;
        p += 5;
        variableBar.setValue(p);
        int i5;
        p += 5;
        variableBar.setValue(p);
        int i6;
        p += 5;
        variableBar.setValue(p);
        int i7;
        p += 5;
        variableBar.setValue(p);
        int i8;
        p += 5;
        variableBar.setValue(p);
        int i9;
        p += 5;
        variableBar.setValue(p);
        int i10;
        p += 5;
        variableBar.setValue(p);
        int i11;
        p += 5;
        variableBar.setValue(p);
        int i12;
        p += 5;
        variableBar.setValue(p);
        int i13;
        p += 5;
        variableBar.setValue(p);
        int i14;
        p += 5;
        variableBar.setValue(p);
        int i15;
        p += 5;
        variableBar.setValue(p);
        int i16;
        p += 5;
        variableBar.setValue(p);
        int i17;
        p += 5;
        variableBar.setValue(p);
        int i18;
        p += 5;
        variableBar.setValue(p);
        int i19;
        p += 5;
        variableBar.setValue(p);
        int i20;
        p += 5;
        variableBar.setValue(p);
    }
    private F() {
    }
    public void actionPerformed(ActionEvent e) {
        new Thread(new F()).start();
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You have two critical bugs, noted in the answers below. – chrylis -cautiouslyoptimistic- Sep 13 '13 at 01:44
  • Make use of a `SwingWorker` to execute your task in the background and update the UI within the Event Dispatching Thread...[*example*](http://stackoverflow.com/questions/16937997/java-swingworker-thread-to-update-main-gui/16938228#16938228), [*example*](http://stackoverflow.com/questions/15199091/progress-bar-java/15199220#15199220), [*example*](http://stackoverflow.com/questions/12020949/jprogressbar-isnt-progressing/12021971#12021971), [*example*](http://stackoverflow.com/questions/17101372/how-to-include-a-progress-bar-in-a-program-of-file-transfer-using-sockets-in-jav/17101753#17101753) – MadProgrammer Sep 13 '13 at 01:53
  • [*example*](http://stackoverflow.com/questions/15668715/how-to-use-the-swing-timer-to-delay-the-loading-of-a-progress-bar/15669717#15669717), [*example*](http://stackoverflow.com/questions/12185924/show-progress-during-ftp-file-upload-in-a-java-applet/12186144#12186144) – MadProgrammer Sep 13 '13 at 01:55

2 Answers2

3

You're creating a new F object in your actionPerformed method. The JProgressBar in that unshown F object is progressing, but not in the one you're displaying.

Try changing this:

new Thread(new F()).start();

to this:

new Thread(this).start();

and see what happens.

As an aside, you're making Swing calls from a background thread, something that you should most definitely not be doing. Rather, use a SwingWorker, set its progress property, and then listen to that property on the Swing event thread using a PropertyChangeListener.

Aside number 2: Your background thread as written will complete instantaneously, and so you won't see any gradual progress but rather instantaneous progress to completion.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I am just trying to practice this loading bar thing for once I start making games and stuff which takes long to load. – user2639281 Sep 13 '13 at 03:52
1

When an ActionEvent comes in, you're firing off your run() method on a new Thread--not the Event Dispatching Thread, where all UI updates have to happen.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152