Question

I'm not newbie but also prof. on Java. I'm trying to add progressBar to my application which send pings to given ip range with isReachable() method. How can i add? I dont know task and thread usage. I read java docs about progressBar but ı couldnt add. Here is my code

final JButton btnScan = new JButton("Scan");
btnScan.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        Runtime rt = Runtime.getRuntime();
        String lastIpCheck = " "; 
        String ip = textField.getText();
        String lastIp = textField_1.getText();
        String parsedOutput=  " ";
        InetAddress inet;
        boolean reachable;

        while(!(lastIpCheck.equalsIgnoreCase(lastIp))) {
            try {
                inet = InetAddress.getByName(ip);
                reachable = inet.isReachable(2500);
                String output=null;
                lastIpCheck = f.nextIpAddress(ip);

                if(reachable) {
                    model.addElement(ip);
                }

                ip = lastIpCheck;

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
});

I want to add progressBar to for Scan Operation, While Loop doing Scan opearation by pinging.

Please Help me. Thank you. Sorry for bad language

Was it helpful?

Solution

For what can I see about your post, you are trying to keep the UI responsive while trying to do a long-running task in Swing.

Unfortunately, Swing is a single thread window system and trying to make long-running tasks will block the UI.

Since Java 1.6 the Swing SDK includes a class called SwingWorker, which allows to execute that kind of tasks in another thread while providing a hook to the UI Thread in order to keep the user updated about how the process is going.

The basic example is given in the Java Tutorial.

SwingWorker worker = new SwingWorker<ImageIcon[], Void>() {
@Override
public ImageIcon[] doInBackground() {
    final ImageIcon[] innerImgs = new ImageIcon[nimgs];
    for (int i = 0; i < nimgs; i++) {
        innerImgs[i] = loadImage(i+1);
    }
    return innerImgs;
}

@Override
public void done() {
    //Remove the "Loading images" label.
    animator.removeAll();
    loopslot = -1;
    try {
        imgs = get();
    } catch (InterruptedException ignore) {}
    catch (java.util.concurrent.ExecutionException e) {
        String why = null;
        Throwable cause = e.getCause();
        if (cause != null) {
            why = cause.getMessage();
        } else {
            why = e.getMessage();
        }
        System.err.println("Error retrieving file: " + why);
    }
  }
};

Basically you can define your own SwingWorker to execute the Ping request in the doInBackgroundMethod and keep updating the UI with the method get()

Here is the link to the Java tutorial: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/simple.html

In the Wikipedia there is a detailed explanation on how to use the SwingWorker: http://en.wikipedia.org/wiki/SwingWorker

I Hope this can help you to solve your problem.

Best Regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top