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
| public class Main extends Application {
private TableView<banned> tableView;
private TableColumn<banned, String> ipAddrTable;
private TableColumn<banned, String> reasonTable;
private TableColumn<banned, String> dateTable;
@Override
public void start(final Stage primaryStage) {
// Normalement ces initialisations sont faites par le FXMLLoader dans ton cas.
tableView = new TableView();
ipAddrTable = new TableColumn<>("ipaddress");
reasonTable = new TableColumn<>("reasonforban");
dateTable = new TableColumn<>("date");
// La plupart de ces instructions devraient être soit dans le FXML soit dans la méthode initialize() du contrôleur dans ton cas.
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
tableView.getColumns().setAll(ipAddrTable, reasonTable, dateTable);
ipAddrTable.setCellValueFactory(new PropertyValueFactory<banned, String>("ipaddress"));
reasonTable.setCellValueFactory(new PropertyValueFactory<banned, String>("reasonforban"));
dateTable.setCellValueFactory(new PropertyValueFactory<banned, String>("date"));
//
final Button addButton = new Button();
addButton.setText("Add");
// Ce gestionnair d'évent est injecté implicitement par le FXMLLoader.
addButton.setOnAction(actionEvent -> addbanlist(null));
final Button deleteButton = new Button();
deleteButton.setText("Delete");
// Ce gestionnair d'évent est injecté implicitement par le FXMLLoader.
deleteButton.setOnAction(actionEvent -> delbanlist(null));
final VBox root = new VBox();
root.getChildren().setAll(addButton, deleteButton, tableView);
//
final Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
}
public void addbanlist(ActionEvent event) {
String ipaddress;
String reasonforban;
TextInputDialog dialogip = new TextInputDialog("0.0.0.0");
dialogip.setTitle("");
dialogip.setContentText("IP:");
Optional<String> ip = dialogip.showAndWait();
ipaddress = ip.get();
TextInputDialog dialogreason = new TextInputDialog("Flood.");
dialogreason.setTitle("");
dialogreason.setContentText("Reason:");
Optional<String> reason = dialogreason.showAndWait();
reasonforban = reason.get();
ObservableList<banned> data = tableView.getItems();
String date = getDate();
data.add(new banned(ipaddress, reasonforban, date));
tableView.setItems(data);
}
private String getDate() {
return LocalDateTime.now().toString();
}
public void delbanlist(ActionEvent event) {
final ObservableList<banned> data = tableView.getItems();
data.removeAll(tableView.getSelectionModel().getSelectedItems());
}
public static class banned {
private final String ipaddress;
private final String reasonforban;
private final String date;
public banned(final String ipaddress, final String reasonforban, final String date) {
this.ipaddress = ipaddress;
this.reasonforban = reasonforban;
this.date = date;
}
public String getIpaddress() {
return ipaddress;
}
public String getReasonforban() {
return reasonforban;
}
public String getDate() {
return date;
}
}
public static void main(String[] args) {
launch(args);
}
} |
Partager