I have a SwingWorker which checks periodically for an internet connection and updates a jLabel accordingly.
public class InternetCheckWorker extends SwingWorker<Void, String> {
@Override
protected Void doInBackground() throws InterruptedException {
while (true) {
try (Socket socket = new Socket(HOSTNAME, 80);) {
process("on");
} catch (IOException e) {
process("off");
}
Thread.sleep(500);
}
}
protected void process(String status) {
jInternetConnectionLabel.setText("Connection " + status);
}
}
private static final String HOSTNAME = "www.google.com";
Is there maybe a more proper way of doing it or even a library with net tools (along with database management tools or general utility tools) that could do this and have less custom code littering my project?
All in all I am looking for the most minimalistic way.