Advertisement
Mouamle

Ping

May 12th, 2016
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package mouamle.ping.ui;
  2.  
  3. import java.awt.EventQueue;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedReader;
  7. import java.io.InputStreamReader;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTextArea;
  13. import javax.swing.JTextField;
  14. import javax.swing.ScrollPaneConstants;
  15.  
  16. public class MainUI implements Runnable{
  17.  
  18.     private JFrame frame;
  19.     private JTextField IP;
  20.     private JTextArea Res;
  21.     private String commnandx = "";
  22.    
  23.    
  24.     Thread t = new Thread(this);
  25.    
  26.     public static void main(String[] args) {
  27.         EventQueue.invokeLater(new Runnable() {
  28.             public void run() {
  29.                 try {
  30.                     MainUI window = new MainUI();
  31.                     window.frame.setVisible(true);
  32.                 } catch (Exception e) {
  33.                     e.printStackTrace();
  34.                 }
  35.             }
  36.         });
  37.     }
  38.  
  39.     public MainUI() {
  40.         initialize();
  41.     }
  42.  
  43.     public void runSystemCommand(String command) {
  44.         try {
  45.             Process p = Runtime.getRuntime().exec(command);
  46.             BufferedReader inputStream = new BufferedReader(
  47.                     new InputStreamReader(p.getInputStream()));
  48.  
  49.             String s = "";
  50.             // reading output stream of the command
  51.             while ((s = inputStream.readLine()) != null) {
  52.                 Res.append(s + "\n");
  53.             }
  54.  
  55.         } catch (Exception e) {
  56.            
  57.         }
  58.     }
  59.  
  60.     private void initialize() {
  61.         frame = new JFrame();
  62.         frame.setBounds(100, 100, 450, 300);
  63.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64.         frame.getContentPane().setLayout(null);
  65.  
  66.         JButton Ping = new JButton("ping");
  67.         Ping.addActionListener(new ActionListener() {
  68.             public void actionPerformed(ActionEvent e) {
  69.                 commnandx = "ping " + IP.getText() + " -t";
  70.                 t.start();
  71.             }
  72.         });
  73.         Ping.setBounds(335, 11, 89, 23);
  74.         frame.getContentPane().add(Ping);
  75.  
  76.         IP = new JTextField();
  77.         IP.setBounds(10, 12, 315, 22);
  78.         frame.getContentPane().add(IP);
  79.         IP.setColumns(10);
  80.  
  81.         JScrollPane scrollPane = new JScrollPane();
  82.         scrollPane
  83.                 .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  84.         scrollPane.setBounds(10, 45, 414, 205);
  85.         frame.getContentPane().add(scrollPane);
  86.  
  87.         Res = new JTextArea();
  88.         scrollPane.setViewportView(Res);
  89.     }
  90.  
  91.     @Override
  92.     public void run() {
  93.         runSystemCommand(commnandx);
  94.     }
  95.    
  96.    
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement