这是TableView中的错误(我的配置:Win7、64位,jdk1.8.0_60),或者我做错了方法。
如果我在tableView上调用setCellSelectionEnabled(true)
以能够选择单个单元格,然后我以编程方式调用table.getSortOrder().setAll(idColumn)
来更改排序列,则光标键将停止工作,直到我用鼠标单击tableView为止。呼叫table.getSelectionModel().select(selectedRow, selectedColumn)
也有帮助,但花了我两天时间才发现,但似乎不正确。
请使用SSCCE波纹管和以下步骤来重现该问题:
课下课
按光标键查看它们是否有效
按F2以使用名字列进行排序
使用光标键查看它们不再起作用
单击tableView中一些未选择的单元格
按光标键看它们再次起作用
按F1使用ID排序
使用光标键再次查看它们不再起作用
按F3以使用“姓氏”列进行排序
按光标键可以看到它们再次起作用,因为那是我所说的table.getSelectionModel().select(selectedRow, selectedColumn)
那是一个错误还是仅我一个?
谢谢!
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
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.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TableViewTest extends Application {
private TableView<Person> table;
private TableColumn<Person, Integer> idColumn;
private TableColumn<Person, String> firstNameColumn;
private TableColumn<Person, String> lastNameColumn;
//------------------------------------------------------
@Override
public void start(Stage primaryStage) {
final BorderPane root = new BorderPane();
table = new TableView<>();
table.setItems(createData());
idColumn = new TableColumn<>("ID");
firstNameColumn = new TableColumn<>("First Name");
lastNameColumn = new TableColumn<>("Last Name");
idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
table.setOnKeyReleased(this::tableKeyEvent);
table.getSelectionModel().setCellSelectionEnabled(true);
table.getColumns().addAll(Arrays.asList(idColumn, firstNameColumn, lastNameColumn));
root.setCenter(table);
primaryStage.setOnShown(windowEvent -> {
table.getSelectionModel().select(0, idColumn);
table.requestFocus();
});
final Scene scene = new Scene(root, 400, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
//------------------------------------------------------
private void tableKeyEvent(KeyEvent e) {
if (e.getCode() == KeyCode.F1) {
table.getSortOrder().setAll(idColumn);
}
if (e.getCode() == KeyCode.F2) {
table.getSortOrder().setAll(firstNameColumn);
}
if (e.getCode() == KeyCode.F3) {
table.getSortOrder().setAll(lastNameColumn);
TablePosition selectedPosition = table.getSelectionModel().getSelectedCells().get(0);
int selectedRow = selectedPosition.getRow();
TableColumn selectedColumn = selectedPosition.getTableColumn();
table.getSelectionModel().select(selectedRow, selectedColumn);
}
}
//------------------------------------------------------
public static void main(String[] args) {
launch(args);
}
//------------------------------------------------------
/* HUGE THANKS to the random name generator at: http://random-name-generator.info/random/?n=100&g=1&st=2
It saved my sanity! ;-) */
private ObservableList<Person> createData() {
return FXCollections.observableArrayList(
new Person(0, "Brendan", "Carter"),
new Person(1, "Karl", "Page"),
new Person(2, "Jane", "Zimmerman"),
new Person(3, "Katrina", "Mcbride"),
new Person(4, "Natalie", "Howard"),
new Person(5, "Jeremiah", "Bishop"),
new Person(6, "Tricia", "Norman"),
new Person(7, "Dorothy", "Medina"),
new Person(8, "Leslie", "Clayton"),
new Person(9, "Ernestine", "Gibbs"),
new Person(10, "Donald", "Fuller"),
new Person(11, "Barbara", "Mendez"),
new Person(12, "Jeff", "Keller"),
new Person(13, "Zachary", "Sanchez"),
new Person(14, "Joyce", "Payne"),
new Person(15, "Neil", "Paul"),
new Person(16, "Amanda", "Patton"),
new Person(17, "Roosevelt", "Rios"),
new Person(18, "Hazel", "Wheeler"),
new Person(19, "Ramona", "Jensen"),
new Person(20, "James", "Berry"));
}
//------------------------------------------------------
public static class Person {
private final IntegerProperty id;
private final StringProperty firstName;
private final StringProperty lastName;
Person(Integer id, String firstName, String lastName) {
this.id = new SimpleIntegerProperty(this, "id", id);
this.firstName = new SimpleStringProperty(this, "firstName", firstName);
this.lastName = new SimpleStringProperty(this, "lastName", lastName);
}
public Integer getId() {
return id.get();
}
public void setId(Integer id) {
this.id.set(id);
}
public IntegerProperty idProperty() {
return id;
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
@Override
public String toString() {
return id.get() + " - " + firstName.get() + " " + lastName.get();
}
}
}
最佳答案
我既不能确认也不能证明这是一个错误,因为文档对此无话可说。但是我可以找出原因:
焦点单元格的TableColumn
变为null
。
下面是您的tableKeyEvent
方法的修改版,它演示了该问题(打印出重点突出的列)并应用了解决方法。
private void tableKeyEvent(KeyEvent e) {
TableColumn refocusColumn = null;
if (e.getCode() == KeyCode.F1) {
refocusColumn = table.getFocusModel().getFocusedCell().getTableColumn();
table.getSortOrder().setAll(idColumn);
}
if (e.getCode() == KeyCode.F2) {
refocusColumn = table.getFocusModel().getFocusedCell().getTableColumn();
table.getSortOrder().setAll(firstNameColumn);
}
if (e.getCode() == KeyCode.F3) {
table.getSortOrder().setAll(lastNameColumn);
TablePosition selectedPosition = table.getSelectionModel().getSelectedCells().get(0);
int selectedRow = selectedPosition.getRow();
TableColumn selectedColumn = selectedPosition.getTableColumn();
table.getSelectionModel().select(selectedRow, selectedColumn);
}
if (refocusColumn != null) {
// print focused column for "debugging"
System.out.println(table.getFocusModel().getFocusedCell().getTableColumn());
// focus previously focused column again to fix issue
table.getFocusModel().focus(table.getSelectionModel().getSelectedIndex(), refocusColumn);
}
}