-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingGUI.java
More file actions
142 lines (128 loc) · 5.04 KB
/
Copy pathpingGUI.java
File metadata and controls
142 lines (128 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//Brian Huang
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.DefaultCaret;
public class pingGUI
{
private JFrame mainFrame; //initializing objects
private JLabel headerLabel;
private JLabel statusLabel;
private JLabel pingNumLabel;
private JPanel controlPanel;
private JTextField inputIP;
private JTextField numPing;
private JButton pingButton;
private JTextArea textArea;
private JScrollPane scroll;
//private JCheckBox continuous;
public pingGUI(){
prepareGUI(); //calling prepare GUI method
}
public static void main(String[] args){ //displaying window
pingGUI window = new pingGUI();
window.showEvent();
}
private void prepareGUI(){
mainFrame = new JFrame("Ping Test"); //creating mainFrame
mainFrame.setSize(400,450);
mainFrame.setLayout(new GridLayout(4, 0));
mainFrame.setResizable(false);
headerLabel = new JLabel("",JLabel.CENTER); //creating labels
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setVerticalAlignment(0);
statusLabel.setSize(350,100);
pingNumLabel = new JLabel("<html><center># Pings<br>(Default = 4)</center></html>");
//pingNumLabel.setHorizontalAlignment(JLabel.RIGHT);
mainFrame.addWindowListener(new WindowAdapter() { //including an exit button
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel(); //creating control panel
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel); //adding labels and panel, setting visible
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true); //setting main frame to visible
}
private void showEvent() { //setting object values
headerLabel.setText("Enter a IP address to ping.");
inputIP = new JTextField(25);
pingButton = new JButton("Ping");
numPing = new JTextField(3);
//continuous = new JCheckBox("Ping continuously?", false);
textArea = new JTextArea(); //setting text area properties
textArea.setColumns(10);
textArea.setRows(10);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
DefaultCaret caret = (DefaultCaret)textArea.getCaret(); //caret for auto scroll down
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
//continuous.setVisible(true);
scroll = new JScrollPane(textArea); //scroll bar
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pingButton.setActionCommand("Ping"); //setting command sent to ButtonClickListener method
pingButton.addActionListener(new ButtonClickListener());
controlPanel.add(pingButton); //adding buttons and labels to control panel
controlPanel.add(inputIP);
//controlPanel.add(continuous);
controlPanel.add(pingNumLabel);
controlPanel.add(numPing);
mainFrame.add(scroll);
mainFrame.setVisible(true); //setting visible
}
/*
* Accepts string as argument. String in this case is the ping information.
* Appends to textArea
*/
public void printIP(String s) {
textArea.append(s + "\n");
}
/*
* Class for accepting commands from button clicks
*/
private class ButtonClickListener implements ActionListener {
/*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
* Accepts ActionEvents when buttons are clicked.
*/
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
String numP = numPing.getText(); //setting numP value to value of numPing text field
if (numPing.getText().isEmpty()) //if left blank, ping # set to 4 (default)
numP = "4";
if (command.equals( "Ping" )) { //if "Ping" command is received from the ping button
if (!inputIP.getText().isEmpty()) {
/*if (continuous.isSelected()) {
statusLabel.setText("Pinging IP Address until connection is stopped...");
runPing("ping " + inputIP.getText() + " -t ");
*/
runPing("ping " + inputIP.getText() + " -n " + numP); //send command if IP address text field is not empty
statusLabel.setText("Ping another IP?");
}
else //message displayed when IP text field is left empty
statusLabel.setText("No IP address entered.");
}
}
}
/*
* method that accepts command in the form of a string. Uses the command prompt functions to ping IP address and return ping info.
*/
public void runPing(String command) { //Ping user specified IP address
try {
Process p = Runtime.getRuntime().exec(command); //accepts a command and returns according to command received.
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = "";
while ((str = input.readLine()) != null) {
printIP(str); //calling method to append text to text area
}
} catch (Exception e){
e.printStackTrace();
}
}
}