【发布时间】:2018-01-01 14:30:36
【问题描述】:
我遇到了 JavaFX TableView 没有触发选择回调的问题。我设置了这两个回调:
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableView.getSelectionModel().selectedItemProperty().addListener((observable) -> {
System.out.println(LocalDateTime.now() + " Item selection changed.");
});
tableView.getSelectionModel().selectedIndexProperty().addListener(observable -> {
System.out.println(LocalDateTime.now() + " Item index changed.");
});
有一件事让我感到困惑。选择更改事件仅在第一次选择和开始多选时触发。每次更改似乎都会发生索引更改。这是为什么呢?
然而,我最大的问题是,当我选择多个条目,然后选择我点击的最后一个条目时,因此,撤消多选但不更改焦点项目时,不会触发任何事件。希望这能说明问题:
任何想法为什么会发生这种情况?我怎么知道选择什么时候发生了这样的变化?
我的最小示例是四个文件:
Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
记录.java
package sample;
import javafx.beans.property.SimpleStringProperty;
public class Record {
private SimpleStringProperty c1 = new SimpleStringProperty();
private SimpleStringProperty c2 = new SimpleStringProperty();
public Record(String c1, String c2) {
this.c1.setValue(c1);
this.c2.setValue(c2);
}
public String getC1() {
return c1.get();
}
public SimpleStringProperty c1Property() {
return c1;
}
public void setC1(String c1) {
this.c1.set(c1);
}
public String getC2() {
return c2.get();
}
public SimpleStringProperty c2Property() {
return c2;
}
public void setC2(String c2) {
this.c2.set(c2);
}
@Override
public String toString() {
return String.format("Record{c1=%s, c2=%s}", c1, c2);
}
}
Controller.java
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import java.time.LocalDateTime;
public class Controller {
private ObservableList<Record> records = FXCollections.observableArrayList();
@FXML
private TableView<Record> tableView;
@FXML
private TableColumn<Record, String> c1Column;
@FXML
private TableColumn<Record, String> c2Column;
@FXML
private void initialize() {
records.add(new Record("c1 - 1", "c2 - 1"));
records.add(new Record("c1 - 2", "c2 - 2"));
records.add(new Record("c1 - 3", "c2 - 3"));
records.add(new Record("c1 - 4", "c2 - 4"));
records.add(new Record("c1 - 5", "c2 - 5"));
tableView.setItems(records);
c1Column.setCellValueFactory(new PropertyValueFactory<>("c1"));
c2Column.setCellValueFactory(new PropertyValueFactory<>("c2"));
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableView.getSelectionModel().selectedItemProperty().addListener((observable) -> {
System.out.println(LocalDateTime.now() + " Item selection changed.");
});
tableView.getSelectionModel().selectedIndexProperty().addListener(observable -> {
System.out.println(LocalDateTime.now() + " Item index changed.");
});
}
}
和sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="c1Column" prefWidth="75.0" text="C1"/>
<TableColumn fx:id="c2Column" prefWidth="75.0" text="C2"/>
</columns>
</TableView>
</StackPane>
【问题讨论】:
标签: java events javafx tableview