Google

Monday, February 9, 2009

IP Scanner 001 - Multi-threading but delayed GUI update



/*
* GUI version of IPScanner
* Scans IP networks using ping
* by Paul Chin, Feb 9, 2009
*/
package ipscannergui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;

public class IPScannerGui extends JFrame {

private JButton btnStart;
private JTextField tfIP;
private JLabel lbIP;
private JTextArea taOutput;
private JScrollPane jsp;

public static void main(String[] args) {
IPScannerGui sg = new IPScannerGui();
sg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sg.setSize(500, 400);
sg.setVisible(true);

}

public IPScannerGui() {
super("IP Scanner 001");
setLayout(new FlowLayout()); // set frame layout
lbIP = new JLabel("Enter network (eg 118.100.118)");
tfIP = new JTextField("118.100.118",20);
btnStart = new JButton("Start");
taOutput = new JTextArea(20, 40);
jsp = new JScrollPane(taOutput);
add(lbIP);
add(tfIP);
add(btnStart);
add(jsp);
ButtonHandler handler = new ButtonHandler();
btnStart.addActionListener(handler);
}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnStart) {
try {

doScan();
} catch (Exception x) {
x.printStackTrace();
}
}
}
}

private void doScan() throws InterruptedException {
Thread[] t = new Thread[255];
MyScanner[] ms = new MyScanner[255];
String net= tfIP.getText() + "."; //Construct net addr eg 192.168.1.
for (int i = 1; i < ip =" ip;" taresult =" (JTextArea)" p =" Runtime.getRuntime().exec(" input =" new" line =" input.readLine())"> -1) {
System.out.println(line);
taResult.append(line + "\n");
}
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}