-1

My progress bar doesn't update until the loop has finished? Why is this?

for (String theURL : IPArray) {
    URL url = new URL(theURL);
    InetAddress address = InetAddress.getByName(url.getHost());
    String temp = address.toString();
    String IP = temp.substring(temp.indexOf("/") + 1, temp.length());
    URLArray.add(IP);
    Progress.percentage = (URLArray.size() * 100) / Progress.totalToDo;
    Progress.ipProgress.setString(Progress.percentage + "%");
    Progress.ipProgress.setValue(Progress.percentage);
    Progress.ipProgress.repaint();
    result += IP + System.getProperty("line.separator");
}

It will only update after it gets past the loop and not during it.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
TehBawz
  • 29
  • 1
  • 6

1 Answers1

0

Needed a new thread.

new Thread(new Runnable() {
    String result = "";

    public void run() {
        for (String theURL : IPArray) {
            try {
                URL url = new URL(theURL);
                InetAddress address = InetAddress.getByName(url.getHost());
                String temp = address.toString();
                String IP = temp.substring(temp.indexOf("/") + 1, temp.length());
                URLArray.add(IP);
                Progress.percentage = (URLArray.size() * 100) / Progress.totalToDo;
                Progress.ipProgress.setString(Progress.percentage + "%");
                Progress.ipProgress.setValue(Progress.percentage);
                Progress.ipProgress.repaint();
                result += IP + System.getProperty("line.separator");
            } catch (Exception e) {
                if ("www.".equals(e.getMessage())) {
                    JOptionPane.showMessageDialog(
                            null, "Incorrect URL. Usage: http://www.URL.com\nError = Space! Can't use gaps in list.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        IPFrame.textAreaIP.setText(result);
        GEOLookup.check(IPFrame.textAreaIP.getText());
    }
}).start();
TehBawz
  • 29
  • 1
  • 6
  • You may be interested in [[this](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html)] official tutorial (especially about SwingWorker class). Also [[here](http://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java)] you have some info about this class. – Pshemo Jul 29 '13 at 12:19
  • will be works with setString & setValue wrapped into invokeLater, common and basic stuff for output from Runnable#Thread, reapint is very hard to already visible Swing GUI, ditry hack that invoked EDT from all corners, in compare with lightweight invokeLater – mKorbel Jul 29 '13 at 12:42