Javafx 如何编辑/打开表中的精确单元格

Javafx 如何编辑/打开表中的精确单元格,javafx,scenebuilder,Javafx,Scenebuilder,这个表是fxml。 列:名字、姓氏、电话、电子邮件 我想在电子邮件栏上单击鼠标,以便编辑准确的电子邮件。 以下代码工作不正常,请始终打开电子邮件进行编辑,而不是精确的单元格 table.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() { 下面是如何在表视图中编辑列的简单示例。您可以使用文本字段列,当您单击该列时,它将允许输入数据。在这种情况下,只有电子邮件列是可编辑的 import javaf

这个表是fxml。 列:名字、姓氏、电话、电子邮件 我想在电子邮件栏上单击鼠标,以便编辑准确的电子邮件。 以下代码工作不正常,请始终打开电子邮件进行编辑,而不是精确的单元格

 table.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() {

下面是如何在表视图中编辑列的简单示例。您可以使用文本字段列,当您单击该列时,它将允许输入数据。在这种情况下,只有电子邮件列是可编辑的

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();



    }

}

代码的其余部分在哪里?完成了吗?是的,完成了。我有fxml名称电子邮件,我只想使用此列,我在Internet中找不到如何执行它的代码不使用fxml场景,当我使用SceneBuilder代码时,我使用另一个JPanel@AlexAs:我想知道table.setOnMouseClickednew EventHandler行在哪里{是在当时放置的…如果您使用顶级类cellFactory,那么在fxml中组装部件也很容易。另外,上次我检查时,SceneBuilder不允许您选择cellValueFactory/cellFactory,因此如果您仅依赖SceneBuilder生成的fxml,您无论如何都无法完成任务。。。
table.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() {
// 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);