Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFXTableView中的导航_Java_Javafx_Tableview - Fatal编程技术网

JavaFXTableView中的导航

JavaFXTableView中的导航,java,javafx,tableview,Java,Javafx,Tableview,我有以下代码生成一个表视图 public class NavExample extends Application { private final TableView<Person> table = new TableView<>(); private final ObservableList<Person> data = FXCollections.observableArrayList(new Person("Z"

我有以下代码生成一个
表视图

public class NavExample extends Application {

    private final TableView<Person> table = new TableView<>();
    private final ObservableList<Person> data
            = FXCollections.observableArrayList(new Person("Z", "X"), new Person("A", "B"));

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setWidth(450);
        stage.setHeight(550);

        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<>("firstName"));

        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<>("lastName"));

        table.getSelectionModel().setCellSelectionEnabled(true);
        table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol);

        table.getSelectionModel().selectFirst();
        table.getFocusModel().focus(table.getSelectionModel().getSelectedIndex());

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;

        private Person(String fName, String lName) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }
    }
}
启动应用程序后

导航(向上和向下键)直到我在
表中选择后才起作用(尽管我保持焦点
表.getFocusModel().focus(table.getSelectionModel().getSelectedIndex());

此外,如果在单击“向下或向上”时选择了一行(通过按住shift键),则不会保留行选择,而是保留下一行的单元格 正在被选中


如何解决这个问题?

一个更简单的解决方案是请求关注表本身,而不是特定的行/单元格:

table.requestFocus();

如果使用单元格选择模式,则需要聚焦单元格而不是行,因此需要指定要聚焦的
TableColumn

// table.getFocusModel().focus(table.getSelectionModel().getSelectedIndex());
table.getFocusModel().focus(table.getSelectionModel().getSelectedIndex(), table.getColumns().get(0));

谢谢,这解决了焦点问题。导航时是否可以保留行选择?(现在不保留行选择)@user3164187您不能,因为您启用了单元格选择。
// table.getFocusModel().focus(table.getSelectionModel().getSelectedIndex());
table.getFocusModel().focus(table.getSelectionModel().getSelectedIndex(), table.getColumns().get(0));