table.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() {
устанавливает обработчик щелчка для всей TableView
вместо этого. Таким образом, вы не можете различать столбцы, которые были нажаты.
Вы должны использовать функцию редактирования, предоставленную TableCell
вместо:
// Simplified Person class
public class Person {
private final StringProperty name;
private final StringProperty email;
public Person(String name, String email) {
this.email = new SimpleStringProperty(email);
this.name = new SimpleStringProperty(name);
}
public final String getEmail() {
return this.email.get();
}
public final void setEmail(String value) {
this.email.set(value);
}
public final StringProperty emailProperty() {
return this.email;
}
public final String getName() {
return this.name.get();
}
public final void setName(String value) {
this.name.set(value);
}
public final StringProperty nameProperty() {
return this.name;
}
}
TableView<Person> table = new TableView<>(FXCollections.observableArrayList(
new Person("Darth Vader", "[email protected]"),
new Person("James Bond", "[email protected]")));
table.setEditable(true);
Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory = col
-> new TableCell<Person, String>() {
{
// make cell itself editable
setEditable(true);
}
@Override
public void startEdit() {
super.startEdit();
// open dialog for input when the user edits the cell
TextInputDialog dialog = new TextInputDialog(getItem());
dialog.setGraphic(null);
dialog.setHeaderText("Set new " + col.getText() + ".");
dialog.setTitle("Edit " + col.getText());
Optional<String> opt = dialog.showAndWait();
if (opt.isPresent()) {
commitEdit(opt.get());
} else {
cancelEdit();
}
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : item);
}
};
TableColumn<Person, String> name = new TableColumn<>("name");
name.setCellValueFactory(p -> p.getValue().nameProperty());
name.setCellFactory(cellFactory);
TableColumn<Person, String> email = new TableColumn<>("email");
email.setCellValueFactory(p -> p.getValue().emailProperty());
email.setCellFactory(cellFactory);
table.getColumns().addAll(name, email);
Где остальная часть кода? это полно? –
YEs это полный. У меня есть имя электронной почты fxml, и я хочу использовать только этот столбец, и я не могу найти в intertet, как я могу это сделать. –