infinite for loop problem
hello there
i'm trying to make a ping program that if the user presses a button
the program pings on a server continusely and print the response
so i used the infinite for loop but when i'm trying to exit the program
it doesn,t exit?how to fix that? here is my code
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
publicclass ReachableTest2extends JFrame{
publicstaticvoid main(String args[]){
JButton btn=new JButton("Check The Ping");
JFrame frame =new JFrame("Ping Check");
frame.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent ev){
System.exit(0);
}
});
ActionListener actionlistener=new ActionListener(){
publicvoid actionPerformed(ActionEvent evnt){
if(evnt.getActionCommand().equals("Check The Ping"))
{
try{
InetAddress address = InetAddress.getByName("212.122.224.10");
System.out.println("Name: " + address.getHostName());
System.out.println("Addr: " + address.getHostAddress());
for( ; ; )
{
System.out.println("Reach: " + address.isReachable(3000));
}
}
catch (UnknownHostException e){
System.err.println("Unable to lookup ");
}
catch (IOException e){
System.err.println("Unable to reach ");
}
}
}
};
frame.add(btn);
btn.addActionListener(actionlistener);
frame.setSize(250,150);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Your actionPerformed method is called in AWT's event dispatcher thread.
Your infinite for loop blocks it from listening to any window event. You
should start a new thread for your pings which can be stopped from
the AWT event thread (or any other thread for that matter). Change your
infinite loop to something like this:
boolean stopped= false;
while (!stopped)
// do your pings here
And add a method to your thread which can be invoked from other threads:
public void setStopped() { this.stopped= true; }
kind regards,
Jos
JosAHa at 2007-7-28 18:33:44 >

i've changed the loop code
but i don't know where to use this mehtod
public void setStopped() { this.stopped= true; }
i tried to use it in the windowlistener but it didnt work
frame.addWindowListener(new WindowAdapter() {
public void setStopped() { this.stopped= true; }
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
where i should use it?
hi friend ,
i have made some changes to your code ,i hope this might help let me know if it works for you
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class test extends JFrame {
public static void main(String args[]) {
JButton btn=new JButton("Check The Ping");
JFrame frame = new JFrame("Ping Check");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
ActionListener actionlistener=new ActionListener() {
public void actionPerformed(ActionEvent evnt) {
if(evnt.getActionCommand().equals("Check The Ping"))
{
ping();
}
}
};
frame.add(btn);
btn.addActionListener(actionlistener);
frame.setSize(250,150);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void ping()
{
Runnable r = new pinging();
Thread t = new Thread(r);
t.start();
}
}
class pinging implements Runnable {
public pinging()
{}
public void run()
{
try {
InetAddress address = InetAddress.getByName("212.122.224.10");
System.out.println("Name: " + address.getHostName());
System.out.println("Addr: " + address.getHostAddress());
for( ; ; )
{
System.out.println("Reach: " + address.isReachable(3000));
}
}
catch (UnknownHostException e) {
System.err.println("Unable to lookup ");
}
catch (IOException e) {
System.err.println("Unable to reach ");
}
}
}
Regards