Aktualisieren Sie JLabel wiederholt mit den Ergebnissen einer lang laufenden Aufgabe

Ich schreibe ein Programm, das ständig einen Server anpingt. Ich habe den Code geschrieben, um ihn einmal zu überprüfen und den Ping in a zu setzenJLabel und setzen Sie es in eine Methode namenssetPing().

Hier ist mein Code

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    setPing();
}           

Das hat funktioniert, aber nur einmal, also habe ich getan:

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
for(;;){
    setPing();
    }
}           

Dies funktioniert aber nicht einmal zum ersten Mal.

Ich habe die setPing-Methode nicht angegeben, weil sie zu lang war. Hier ist sie:

public String setPing(){
Runtime runtime = Runtime.getRuntime(); 
try{
    Process process = runtime.exec("ping lol.garena.com");
InputStream is = process.getInputStream(); 
InputStreamReader isr = new InputStreamReader(is); 
BufferedReader br = new BufferedReader(isr); 
String line; 
while ((line = br.readLine()) != null) {
    int i = 0;
      i = line.indexOf("Average");
    if(i > 0){  
    String finalPing = "";
    line.toCharArray();
    try
    {
        finalPing = "";
        for(int x = i; x < i + 17; x++)
        {
            finalPing = finalPing + (line.charAt(x));
        }
    }catch(IndexOutOfBoundsException e)
    {
        try
        {
            finalPing = "";
            for(int x = i; x < i + 16; x++)
            {
                finalPing = finalPing + (line.charAt(x));
            }
        }catch(IndexOutOfBoundsException f)
        {
            try
            {
                finalPing = "";
                for(int x = i; x < i + 15; x++)
                {
                    finalPing = finalPing + (line.charAt(x));
                }
            }catch(IndexOutOfBoundsException g){}
        }
    }
    String final1Ping = finalPing.replaceAll("[^0-9]", "");
    return final1Ping;
    }
} 
}catch(IOException e){
}
return "";
}

AKTUALISIEREN Nur für den Fall, dass dies wichtig ist, verwende ich Netbeans. Ich habe ein Formular erstellt und diesen Code in formWindowOpened evt eingefügt, anstatt ihn in main aufzurufen:

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  

    ActionListener timerListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            new PingWorker().execute();
        }
    };
    Timer timer = new Timer(1000, timerListener);


        timer.start();
        jLabel1.setText(label.getText());
        timer.stop();
 // TODO add your handling code here:
}                                 

class PingWorker extends SwingWorker {

    int time;

    @Override
    protected Object doInBackground() throws Exception {
        time = pingTime("lol.garena.com");
        return new Integer(time);
    }

    @Override
    protected void done() {
        label.setText("" + time);
    }
};

public JComponent getUI() {
    return label;
}

public static int pingTime(String hostnameOrIP) {
    Socket socket = null;
    long start = System.currentTimeMillis();
    try {
        socket = new Socket(hostnameOrIP, 80);
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
            }
        }
    }
    long end = System.currentTimeMillis();
    return (int) (end - start);
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage