表格是fxml。 列:名字,姓氏,电话,电子邮件 我想在电子邮件列中点击鼠标,以便编辑确切的电子邮件。 以下代码工作不正常,始终打开要编辑的电子邮件,而不是确切的单元格
table.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() {
答案 0 :(得分:0)
以下是如何在表格视图中编辑列的简单示例。您可以使用文本字段列,这将允许您在单击时输入数据。在这种情况下,只有电子邮件列可以编辑。
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFXApplication7 extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Person> tv = new TableView();
TableColumn<Person, String> fCol = new TableColumn<>();
TableColumn<Person, String> lCol = new TableColumn<>();
TableColumn<Person, String> pCol = new TableColumn<>();
TableColumn<Person, String> eCol = new TableColumn<>();
tv.setEditable(true);
tv.getColumns().addAll(fCol, lCol, pCol, eCol);
fCol.setCellValueFactory(data -> data.getValue().firstName);
lCol.setCellValueFactory(data -> data.getValue().lastName);
pCol.setCellValueFactory(data -> data.getValue().phone);
eCol.setCellValueFactory(data -> data.getValue().email);
eCol.setCellFactory(tc -> new TextFieldTableCell<>());
ObservableList<Person> items = FXCollections.observableArrayList();
Person p = new Person();
p.email.set("foo@bar.com");
p.firstName.set("Tony");
p.lastName.set("Stark");
p.phone.set("(555) 555-1212");
items.add(p);
tv.setItems(items);
StackPane root = new StackPane();
root.getChildren().add(tv);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public class Person {
StringProperty firstName = new SimpleStringProperty();
StringProperty lastName = new SimpleStringProperty();
StringProperty phone = new SimpleStringProperty();
StringProperty email = new SimpleStringProperty();
}
}
答案 1 :(得分:0)
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", "dad@empire.com"),
new Person("James Bond", "bond007@mi6.gb")));
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);