Javafx 2 如何在javaFX中二次单击/选择时取消选择选定表行

Javafx 2 如何在javaFX中二次单击/选择时取消选择选定表行,javafx-2,javafx-8,Javafx 2,Javafx 8,在我的JavaFX表中,当我单击一行时,它会选择该行。现在,当我第二次单击之前选中的同一行时,我想取消选中该行。可能吗?如果可能的话,请分享一些示例代码。基本上,您可以选择任何无效的内容作为索引。通常首选-1 table.getSelectionModel().select(-1); 它调用intselect。备选方案: table.getSelectionModel().select(null); 它调用对象select 如果您想查看此应用的全部代码/确认 public class Mai

在我的JavaFX表中,当我单击一行时,它会选择该行。现在,当我第二次单击之前选中的同一行时,我想取消选中该行。可能吗?如果可能的话,请分享一些示例代码。

基本上,您可以选择任何无效的内容作为索引。通常首选
-1

table.getSelectionModel().select(-1);
它调用int
select
。备选方案:

table.getSelectionModel().select(null);
它调用对象
select

如果您想查看此应用的全部代码/确认

public class Main extends Application {
    @SuppressWarnings("unchecked")
    @Override
    public void start(Stage stage) {        
        Scene scene = new Scene(new Group());
        TableView<Person> table = new TableView<Person>();
        stage.setTitle("Table View Sample");
        stage.setWidth(300);
        stage.setHeight(500);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);

        TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("Test Name");
        firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("name"));

        table.getColumns().addAll(firstNameCol);

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

        table.itemsProperty().get().add(new Person("Hans"));
        table.itemsProperty().get().add(new Person("Dieter"));

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

        table.getSelectionModel().select(-1);

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

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

    public class Person {
        final StringProperty name = new SimpleStringProperty();

        Person(String name) {
            this.name.set(name);
        }

        public StringProperty nameProperty() { return this.name; }
    }
}
public类主扩展应用程序{
@抑制警告(“未选中”)
@凌驾
公众假期开始(阶段){
场景=新场景(新组());
TableView table=新TableView();
stage.setTitle(“表格视图示例”);
舞台设置宽度(300);
舞台设置高度(500);
最终标签=新标签(“地址簿”);
label.setFont(新字体(“Arial”,20));
table.setEditable(true);
TableColumn firstNameCol=新的TableColumn(“测试名称”);
firstNameCol.setCellValueFactory(新属性ValueFactory(“名称”);
table.getColumns().addAll(firstNameCol);
最终VBox VBox=新的VBox();
vbox.setspace(5);
设置填充(新的插入(10,0,0,10));
vbox.getChildren().addAll(标签、表格);
table.itemsProperty().get().add(新人(“Hans”);
table.itemsProperty().get().add(新人(“节食者”);
((组)scene.getRoot()).getChildren().addAll(vbox);
table.getSelectionModel().select(-1);
舞台场景;
stage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
公共阶层人士{
final StringProperty名称=新的SimpleStringProperty();
Person(字符串名称){
this.name.set(name);
}
public StringProperty nameProperty(){返回this.name;}
}
}

下面的一段代码适用于此要求

tableView.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {  
        @Override  
        public TableRow<Person> call(TableView<Person> tableView2) {  
            final TableRow<Person> row = new TableRow<>();  
            row.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {  
                @Override  
                public void handle(MouseEvent event) {  
                    final int index = row.getIndex();  
                    if (index >= 0 && index < tableView.getItems().size() && tableView.getSelectionModel().isSelected(index)  ) {
                        tableView.getSelectionModel().clearSelection();
                        event.consume();  
                    }  
                }  
            });  
            return row;  
        }  
    });  
tableView.setRowFactory(新回调(){
@凌驾
公共TableRow调用(TableView tableView2){
最终TableRow行=新TableRow();
row.addEventFilter(MouseeEvent.MOUSE_按下,new EventHandler(){
@凌驾
公共无效句柄(MouseEvent事件){
final int index=row.getIndex();
如果(索引>=0&&index

使用了oracle表视图示例中的相同Person类。最初的答案是@James_D在甲骨文论坛上给出的

如果您能通过提及
getSelectionModel().clearAndSelect(int index)
并将“单击选定项时取消选择”作为OP请求来改进您的答案,那将是一件非常好的事情。@thatslch我尝试了上面的代码片段。如果我再次单击上一行,它不会取消选择上一行。@Dil我不理解您的问题:我以为如何清除是您的问题。此实现取消选择所有选定项。要仅取消选择一项,请使用
tableView.getSelectionModel().clearSelection(索引)