rendered paste bodyimport java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.awt.event.*;
import javax.swing.*;
public class NetworkUtil implements ActionListener{
JTextField address;
JComboBox jc;
JTextArea jt;
private static void createAndShowGUI(){
NetworkUtil n = new NetworkUtil();
JFrame jf = new JFrame();
JPanel jp = new JPanel(new FlowLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
n.jc = new JComboBox(new Object[] {"Traceroute","Ping"} );
n.address = new JTextField("127.0.0.1");
n.jt = new JTextArea(20,25);
JScrollPane pane = new JScrollPane(n.jt);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JButton action = new JButton("Run command");
action.addActionListener(n);
jp.add(new JLabel("Type"));
jp.add(n.jc);
jp.add(new JLabel("Address"));
jp.add(n.address);
jp.add(action);
jp.add(pane);
jf.add(jp);
jf.pack();
//n.jt.setPreferredSize(new Dimension(300,500));
jf.setVisible(true);
}
public static void main(String args[])throws Exception{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void actionPerformed(ActionEvent e){
if(jc.getSelectedIndex()==0){
jt.setText("");
Runtime rt = Runtime.getRuntime();
Process proc =
rt.exec("tracert " + address.getText());
BufferedReader b = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
String line;
while ((line = b.readLine()) != null) {
System.out.println(line + " was line");
jt.append(line+"\n");
}
proc.destroy();
b.close();
}
else if(jc.getSelectedIndex()==1){
jt.setText("");
Runtime rt = Runtime.getRuntime();
Process proc =
rt.exec("ping " + address.getText());
BufferedReader b = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
String line;
while ((line = b.readLine()) != null) {
jt.append(line+"\n");
}
proc.destroy();
b.close();
}
}
}
}
}