要么这是TableView中的错误(我的配置:Win7,64bit,jdk1.8.0_60),要么我做错了。
如果我在tableView上调用setCellSelectionEnabled(true)
以便能够选择单个单元格然后我调用table.getSortOrder().setAll(idColumn)
以编程方式更改排序列,则光标键将停止工作,直到我用鼠标单击tableView 。拨打table.getSelectionModel().select(selectedRow, selectedColumn)
也有帮助,但我花了2天的时间才发现它似乎不对。
请使用下面的SSCCE和以下步骤重现问题:
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();
}
}
}
答案 0 :(得分:1)
我既不能证实也不能反驳它是一个错误,因为文档没有任何关于此的说法。但是我可以找出原因:
焦点单元格的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);
}
}