package UDPYT;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTextArea;
public class PingCombobox extends JFrame implements ActionListener {
static DefaultComboBoxModel<String> comboboxModel;
private JTextField textField;
static JComboBox<String> comboBox;
static ArrayList<String> ipList = new ArrayList<String>();
private JButton connect;
static JButton addButton;
static String txt;
private JButton anzeigen;
static JTextArea textArea;
static String ipAdresse;
public PingCombobox() {
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 519, 445);
JPanel fensterPanel = new JPanel();
fensterPanel.setBackground(Color.WHITE);
fensterPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(fensterPanel);
fensterPanel.setLayout(null);
comboBox = new JComboBox();
comboBox.setBounds(187, 92, 232, 22);
getContentPane().add(comboBox);
fensterPanel.add(comboBox);
textField = new JTextField();
textField.setBounds(187, 40, 232, 27);
fensterPanel.add(textField);
textField.setColumns(10);
addButton = new JButton("add");
addButton.setBounds(38, 42, 89, 23);
fensterPanel.add(addButton);
addButton.addActionListener(this);
connect = new JButton("Connect");
connect.setBounds(38, 92, 89, 23);
fensterPanel.add(connect);
anzeigen = new JButton("Anzeigen");
anzeigen.setBounds(38, 150, 89, 23);
fensterPanel.add(anzeigen);
anzeigen.addActionListener(this);
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(187, 151, 232, 190);
fensterPanel.add(textArea);
connect.addActionListener(this);
comboboxModel = new DefaultComboBoxModel<String>();
}
public static void main(String[] args) {
try {
PingCombobox frame = new PingCombobox();
ununterbrochenesAbfragen();
frame.setVisible(true);
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object knopfhorcher = e.getSource();
if (knopfhorcher == addButton) {
ipAdresse=textField.getText();
if (ipAdresse.equals("")) {
textArea.setText("Keine Eingabe.");
}else{
ipAdresse = textField.getText();
comboboxModel.addElement(ipAdresse);
ipList.add(ipAdresse);
textField.setText("");
comboBox.setModel(comboboxModel);}
} else if (knopfhorcher == connect) {
txt = (String) comboBox.getSelectedItem();
} else if (knopfhorcher == anzeigen) {
listeChecken();
}
}
public static void sendPingRequest() throws IOException {
for (int i = 0; i < ipList.size(); i++) {
InetAddress zielAdresse = InetAddress.getByName(ipList.get(i));
System.out.println("Sending Ping Request to " + ipList.get(i));
if (zielAdresse.equals("")) {
System.out.println("noch leer");
} else {
if (zielAdresse.isReachable(5000)) {
System.out.println("Ist angekommen");
} else {
System.out.println("Keine Antwort angekommen");
}
}
}
}
public static void ununterbrochenesAbfragen() {
int minutes = 1;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
sendPingRequest();
} catch (IOException e) {
}
}
}, 0, 100 * 60 * minutes);
}
public static void listeChecken() {
for (int i = 0; i < ipList.size(); i++) {
textArea.append(ipList.get(i) + "\n");
System.out.println(ipList.get(i));
}
}
}