4

I was looking through this thread

How to make Timer countdown along with progress bar?

I would like to add this to my code so i can just get a jProgressBar and a Button, (Using netbeans preferably)

So that when I hit the button it runs from 0 to 100 steadily, I really have tried to go at this on my own and have got really mad, any help would be nice.

Community
  • 1
  • 1
Jack
  • 2,891
  • 11
  • 48
  • 65
  • What is your question? *"Ps please don't use big words"* Does 'Ps' mean post-script or please? – Andrew Thompson Aug 12 '12 at 01:22
  • Seems to me you are a beginner in Swing. If so, better leave the IDE to one side for the time being, and use plain old **TEXT-EDITOR** to make your programs like **JEDIT/NOTEPAD/NOTEPAD++/NOTEPAD 2**. That way you will learn more and quick, rather than using IDE, where half of the work is hidden from your sight. Once you become somewhat good, then you can shift to any IDE of your choice. – nIcE cOw Aug 12 '12 at 16:38
  • Oh cool just seen this, thanks, I just like the gui creater in Netbeans, but i get your meaning i will shift over to a Notepadd++ then, thanks, but also right now i do not want to give up on this project seeing as i spent alot of time on it + on the gui and things. thanks for you reply – Jack Aug 12 '12 at 19:35
  • Sorry for the grave dig but to clarify I meant post-script. – Jack Mar 14 '13 at 16:10

1 Answers1

6

Leveraging @Andrew's example,

image

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

class CountUpProgressBar extends JPanel {

    private JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
    private JLabel label = new JLabel("", JLabel.CENTER);
    private Timer timer = new Timer(100, new ActionListener() {

        private int counter = 1;

        @Override
        public void actionPerformed(ActionEvent ae) {
            label.setText(String.valueOf(counter));
            bar.setValue(++counter);
            if (counter > 100) {
                timer.stop();
            }
        }
    });

    CountUpProgressBar() {
        super.setLayout(new GridLayout(0, 1));
        bar.setValue(0);
        timer.start();
        this.add(bar);
        this.add(label);
        JOptionPane.showMessageDialog(null, this);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                CountUpProgressBar cdpb = new CountUpProgressBar();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also `setStringPainted(true)`. – trashgod Aug 12 '12 at 01:35
  • :-) , is the link to **setStringPainted(true)** missing, or you deliberately left it as such ? – nIcE cOw Aug 12 '12 at 16:39
  • 1
    @GagandeepBali: Ah, you noticed! Yes, I deliberately omitted it, as the question asked for `0..100` rather than a percentage. Oh, you mean a link in the comment? Yes, also deliberate, as I wanted to leave _some_ fun for the poster. – trashgod Aug 12 '12 at 16:51
  • Well at the moment I am still rather confused, Thanks alot for a quick answer ( i am afraid i was out today so did not have time too look it over until now,) Although I wonder if I can ask a few questions ( Most likely very simple and stupid :/) So how would i add this to a NetBeans Project, New project -> Java Application -> now do I need to make a new *main*class ( I assume not due to one being used above ) -> *right click * new java class, Then i am sort of Lost, i really appreciate your help on this :) thanks so much. – Jack Aug 12 '12 at 19:23
  • Create a `File > New File… > Java Class` with the name `CountUpProgressBar`, replace the contents with the example, and click `Run > Run File`. – trashgod Aug 13 '12 at 01:17