0

In first, I'm using Eclipse IDE. So my question is: I create a progress bar, but I want to make it load according to the place where the program is. I have a refresh button, and when I click it my entire program update. What I want is a progress bar that accompanying the process of updating and ends when it ends.

Sorry if my English isn't the best, but I'm a young Portuguese developer.

my btn code

JButton btnActualizar = new JButton("\u21BB");
    btnActualizar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


            TxtIpExternoLoja.setText(" ");
            TxtSSIDLoja.setText(" ");
            TxtFirewallLoja.setText(" ");

            TxtIpLojaEtho.setText(" ");
            TxtMaskLojaEtho.setText(" ");
            TxtGWLojaEtho.setText(" ");
            TxtDns1LojaEtho.setText(" ");
            TxtDns2LojaEtho.setText(" ");

            TxtIpLojaWlan.setText(" ");
            TxtMaskLojaWlan.setText(" ");
            TxtGwLojaWlan.setText(" ");
            TxtDns1LojaWlan.setText(" ");
            TxtDns2LojaWlan.setText(" ");

            TxtIpLojaVpn.setText(" ");
            TxtMaskLojaVpn.setText(" ");
            TxtGwLojaVpn.setText(" ");
            TxtDns1LojaVpn.setText(" ");
            TxtDns2LojaVpn.setText(" ");

            // DefaultTableModel model = (DefaultTableModel)
            // tablePing.getModel();
            // model.setRowCount(0);
            TxtTime.setText(" ");

            i = 0;
            t.start();
            btnActualizar.setEnabled(false);
            update();

        }
    });

my progressbar code:

JProgressBar progressBar = new JProgressBar(0, 15);
    GridBagConstraints gbc_progressBar = new GridBagConstraints();
    gbc_progressBar.insets = new Insets(0, 0, 0, 5);
    gbc_progressBar.gridx = 3;
    gbc_progressBar.gridy = 0;
    PanelBotoes.add(progressBar, gbc_progressBar);
    GridBagConstraints gbc_btnImprimir = new GridBagConstraints();
    gbc_btnImprimir.insets = new Insets(0, 0, 0, 5);
    gbc_btnImprimir.gridx = 5;
    gbc_btnImprimir.gridy = 0;
    PanelBotoes.add(btnImprimir, gbc_btnImprimir);
    progressBar.setStringPainted(true);
    progressBar.setValue(0);

    t = new Timer(interval, new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            if (i == 15) {
                t.stop();
                btnActualizar.setEnabled(true);
            } else {
                i++;
                progressBar.setValue(i);
            }
        }
    });
R.Quintal
  • 43
  • 1
  • 3
  • 13
  • welcome to SO. but can u clarify your question more? what u want to achieve is not clear. Better add some code you have tried – Zahan Safallwa Jan 27 '16 at 10:57
  • Check my post now,please. Thank you – R.Quintal Jan 27 '16 at 12:38
  • Please clarify. "Place where the program is" - what does it mean? What is the process of "updating"? Should the progress bar go from 0% to 100% on each update, or should it make a step each time the button is clicked? – Goblin Alchemist Jan 27 '16 at 14:31
  • "The first one is my button that should be the trigger to start the update! the setText(" ") in there is just to put the field empty. After that the uptade runs and insert text in there again. I apologize if I expressed myself badly but I dont want a Scroll bar but a progress bar! The update button should trigger the progress bar at the same time as the "update" class. So the progress bar could accompanying the update process. Thank for your time and help" – R.Quintal Jan 27 '16 at 14:32
  • 1
    What is the update process? How much time does it take? Why does it need a progress bar? – Goblin Alchemist Jan 27 '16 at 15:36
  • The update process is what I want to do when I press the button "update". It depends! My program should run a lot of pings, it could be 1 ping or 100 pings! So I don't know exactly how much time does it take. It need a progress bar because my boss want it so... – R.Quintal Jan 27 '16 at 15:52

1 Answers1

3

Take a look at this page: https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html.

Specifically, the section on "Using Determinate Progress Bars" should be what you are looking for. The example code is a little complex, but probably about as simple as it can get with this stuff.

Take note: the example uses a SwingWorker (more on those here) to update the progress value. This is because the progress bar is drawn in the Swing thread and won't be updated if the process is running on that thread too. You will need to update your program on a thread which is not the Swing thread.


Edit 1 - For your question update with code

So it looks like you've got the idea with the progress bar code. I've not run it, but what you've got looks like it should work. Your next step is to replace the timer with the update() methods process. To do this you'll need to do the following:

  1. Make sure your update() method is running in a thread separate from the swing thread (something like a SwingWorker could be used here).
  2. From within the update thread you need to call progressBar.setValue(x) when you want to update the bar, where x = how far your process has got.
  3. Add some more calls to progressBar.setValue(x) in the update thread. You can put these wherever you like, but it's probably best to put them before and after long processes.

Note: you have to use threads for progress bars. This means you could run into things like deadlock and race conditions. Be careful!

Community
  • 1
  • 1
Tiz
  • 680
  • 5
  • 17
  • Check my post now. I want to made scroll accompanying my "update" class. Can you help me better? – R.Quintal Jan 27 '16 at 12:38
  • @R.Quintal I can't comment on questions yet, so I'll have to reply here: Is that code the update button you want the scroll bar to be triggered by? Or are the `setText(" ")` calls the updates you want the progress bar to track? I'm not sure I understand what you're asking... – Tiz Jan 27 '16 at 12:47
  • The first one is my button that should be the trigger to start the update! the setText(" ") in there is just to put the field empty. After that the uptade runs and insert text in there again. I apologize if I expressed myself badly but I dont want a Scroll bar but a progress bar! The update button should trigger the progress bar at the same time as the "update" class. So the progress bar could accompanying the update process. Thank for your time and help. – R.Quintal Jan 27 '16 at 13:01
  • @R.Quintal I've updated my answer to account for the additional information! Also, sorry I mentioned scroll bars. I did *mean* "progress" but typed something completely different! My fault. – Tiz Jan 27 '16 at 15:42
  • I understand what you saying, but as I said before I'm not so goos as it. I don't even know how to use SwingWorker. But I'll try to find out. Thank you again. – R.Quintal Jan 27 '16 at 16:04
  • @R.Quintal No problem, hope you sort it out! :) – Tiz Jan 27 '16 at 16:15
  • @R.Quintal `SwingWorkers` are only one option when it comes to threading, but they are quite powerful! you can find info on them [here](http://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java) and [here](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) or just by googling. I'd really recommend you take a look at some of the issue which can arise from threading, and keep an eye out for them! Good luck! – Tiz Jan 27 '16 at 16:20