So im building an application in swing that will ping a url or a list of urls to show the machines status. So if a machine disconnects it will show up as disconnected. I have a list of machines but I need it to loop over forever in intervals so I can know the status of the machine. I know you cant reset the enumeration count so im stuck on a workaround for it.
Currently the part of the code that does the pinging and enumeration is below:
class WorkerThread extends Thread {
DefaultListModel model;
public WorkerThread(DefaultListModel aModel) {
model = aModel;
}
public void run() {
Pinging ping = new Pinging();
int num ;
Enumeration<String> e;
for ( e= model.elements(), num=0; e.hasMoreElements(); num ++){
String element = e.nextElement();
for (int i = 0; i<5; i++){
ping.reachUrl(element);
//ping.timer(5, e.nextElement());
final int pos = num;
EventQueue.invokeLater(new Runnable() {
public void run() {
//ping.reachUrl(e.nextElement());
String name = ping.getName();
String addr = ping.getAddr();
boolean reach = ping.getReach();
int time = ping.getTime();
model.set(pos, name+" " + addr+" " + reach + " " + time);
//model.add(pos, name+" " + addr+" " + reach + " " + time);
//model.set(pos, "Hello");
}
});
yield();
}
}
}
}
Use a List
rather than an Enumeration
.
Maintain an int
that records your place in the list, and when you get to the end of the list, start again at the beginning.
But really you want a separate thread for each URL that you're watching. And you certainly don't want to be doing the ping calls on the event thread. Use a SwingWorker
, so that it can run in the background and still update the GUI using the done()
method when it's finished.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.