import javafx.application.Application; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.collections.FXCollections; import javafx.event.EventHandler; import javafx.scene.control.TableColumn.CellEditEvent; import javafx.scene.control.cell.TextFieldTableCell; public class App extends Application { private TableView table = new TableView<>(); private TextField nameInput, cityInput, salaryInput; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { TableColumn nameCol = new TableColumn<>("Név"); nameCol.setMinWidth(100); nameCol.setCellValueFactory(new PropertyValueFactory<>("name")); TableColumn cityCol = new TableColumn<>("Település"); cityCol.setMinWidth(100); cityCol.setCellValueFactory(new PropertyValueFactory<>("city")); TableColumn salaryCol = new TableColumn<>("Fizetés"); salaryCol.setMinWidth(100); salaryCol.setCellValueFactory(new PropertyValueFactory<>("salary")); nameCol.setCellFactory( TextFieldTableCell.forTableColumn() ); cityCol.setCellFactory( TextFieldTableCell.forTableColumn() ); salaryCol.setCellFactory( TextFieldTableCell.forTableColumn() ); nameCol.setOnEditCommit( new EventHandler>() { @Override public void handle(CellEditEvent t) { ((Employee) t.getTableView().getItems().get( t.getTablePosition().getRow()) ).setName(t.getNewValue()); } } ); cityCol.setOnEditCommit( new EventHandler>() { @Override public void handle(CellEditEvent t) { ((Employee) t.getTableView().getItems().get( t.getTablePosition().getRow()) ).setCity(t.getNewValue()); } } ); salaryCol.setOnEditCommit( new EventHandler>() { @Override public void handle(CellEditEvent t) { ((Employee) t.getTableView().getItems().get( t.getTablePosition().getRow()) ).setSalary(t.getNewValue()); } } ); table.setEditable(true); nameInput = new TextField(); nameInput.setPromptText("Név"); cityInput = new TextField(); cityInput.setPromptText("Település"); salaryInput = new TextField(); salaryInput.setPromptText("Fizetés"); Button addButton = new Button("Hozzáad"); addButton.setOnAction(e->onClickAddButton()); Button delButton = new Button("Törlés"); delButton.setOnAction(e->onClickDelButton()); HBox inputBox = new HBox(10); inputBox.setPadding(new Insets(10, 10, 10, 10)); inputBox.setSpacing(10); inputBox.getChildren().addAll( nameInput, cityInput, salaryInput ); HBox buttonBox = new HBox(10); buttonBox.setPadding(new Insets(10, 10, 10, 10)); buttonBox.setSpacing(10); buttonBox.getChildren().addAll( addButton, delButton ); VBox vbox = new VBox(); vbox.getChildren().addAll(table, inputBox, buttonBox); table.setItems(getEmployees()); table.getColumns().add(nameCol); table.getColumns().add(cityCol); table.getColumns().add(salaryCol); Scene scene = new Scene(vbox, 302, 250); primaryStage.setScene(scene); primaryStage.show(); } private void onClickAddButton() { String name = this.nameInput.getText(); String city = this.cityInput.getText(); double salary = Double.parseDouble(this.salaryInput.getText()); Employee employee = new Employee(name, city, salary); table.getItems().add(employee); this.nameInput.clear(); this.cityInput.clear(); this.salaryInput.clear(); } private void onClickDelButton() { ObservableList selectedEmployee, employees; employees = table.getItems(); selectedEmployee = table.getSelectionModel().getSelectedItems(); selectedEmployee.forEach(employees::remove); } private ObservableList getEmployees() { ObservableList employees = FXCollections.observableArrayList( new Employee("Poros Béla", "Szeged", 2870000), new Employee("Tér Irén", "Szeged", 2435000), new Employee("Pakos Péter", "Hatvan", 2530000), new Employee("Szu Emese", "Szolnok", 2270000)); return employees; } }