Это мой код: < /p>
Код: Выделить всё
public class NewMain1 extends Application {
public static class Person {
private final StringProperty firstName;
private final StringProperty lastName;
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
}
public StringProperty firstNameProperty() { return firstName; }
public StringProperty lastNameProperty() { return lastName; }
}
@Override
public void start(Stage primaryStage) {
TableView
tableView = new TableView();
tableView.setEditable(true);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
tableView.getColumns().addAll(firstNameCol, lastNameCol);
ObservableList data = FXCollections.observableArrayList(
new Person("John", "Smith"),
new Person("Emily", "Johnson"),
new Person("Michael", "Williams"),
new Person("Sarah", "Brown")
);
tableView.setItems(data);
tableView.setRowFactory(tv -> {
TableRow row = new TableRow();
var contextMenu = new ContextMenu(new MenuItem("Show Salary"));
row.emptyProperty().addListener((ov, oldV, newV) -> {
if (newV) {
row.setContextMenu(null);
} else {
row.setContextMenu(contextMenu);
}
});
return row;
});
VBox root = new VBox(tableView);
Scene scene = new Scene(root, 400, 300);
primaryStage.setTitle("Editable Names Table");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... -in-javafx