JavaFX:默认情况下按特定列对TableView进行排序
我有一个TableView,dataTable,它包含五列。我想根据TableColumn scoreColumn对行进行排序,它的类型是Integer。用户可以通过单击scoreColumn标题的箭头来完成此操作,但我希望在默认情况下按降序对数据进行排序 表的记录都是同时加载的,不可更新,所以我不必担心顺序的变化,所以排序只需要执行一次 以下是使用数据以及TableView和TableColumn声明初始化dataTable的方法:JavaFX:默认情况下按特定列对TableView进行排序,java,javafx,tableview,fxml,scenebuilder,Java,Javafx,Tableview,Fxml,Scenebuilder,我有一个TableView,dataTable,它包含五列。我想根据TableColumn scoreColumn对行进行排序,它的类型是Integer。用户可以通过单击scoreColumn标题的箭头来完成此操作,但我希望在默认情况下按降序对数据进行排序 表的记录都是同时加载的,不可更新,所以我不必担心顺序的变化,所以排序只需要执行一次 以下是使用数据以及TableView和TableColumn声明初始化dataTable的方法: ... @FXML private TableView&l
...
@FXML
private TableView<Data> dataTable;
@FXML
private TableColumn<Data, TextFlow> foundPeptideColumn;
@FXML
private TableColumn<Data, String> targetPeptideColumn;
@FXML
private TableColumn<Data, Integer> scoreColumn;
@FXML
private TableColumn<Data, Integer> rowColumn;
@FXML
private TableColumn<Data, Integer> peptideNumColumn;
...
@FXML
private void initialize()
{
// Initialize the data table with the five columns.
foundPeptideColumn.setCellValueFactory(cellData -> cellData.getValue().foundPeptideProperty());
targetPeptideColumn.setCellValueFactory(cellData -> cellData.getValue().peptideProperty());
scoreColumn.setCellValueFactory(cellData -> cellData.getValue().scoreProperty().asObject());
rowColumn.setCellValueFactory(cellData -> cellData.getValue().rowProperty().asObject());
peptideNumColumn.setCellValueFactory(cellData -> cellData.getValue().peptideNumProperty().asObject());
}
...
(根据这个问题的答案:)
(根据这个问题的答案:)
我还尝试了一些类似于上述示例的其他方法(主要是结合放置/顺序/上述尝试修复的组合),但都没有效果
在FXML或SceneBuilder中,我是否还应该尝试通过编程方式根据scoreColumn自动对表进行排序
如果有帮助的话,我可以用我的应用程序的图像更新我的帖子,或者为我的控制器添加额外的代码。您是否使用分类列表作为表格项目的基础? 请参阅第页的“排序”部分 (基于这个问题的答案:JavaFX:TableView:Arrow for 默认情况下排序的列) 我也有同样的问题,我使用了链接的答案 在将数据填入表中后,必须插入这部分代码
dataTable.getColumns().addAll(scoreColumn);
dataTable.getSortOrder().add(scoreColumn);
dataTable.sort();
这对我很有用。如果分数列位于
FXML
中,是否需要dataTable.getColumns().addAll(scoreColumn)代码>请。。
@FXML
private void initialize()
{
scoreColumn.setSortType(TableColumn.SortType.DESCENDING);
dataTable.getSortOrder().add(scoreColumn);
// Initialize the data table with the five columns.
foundPeptideColumn.setCellValueFactory(cellData -> cellData.getValue().foundPeptideProperty());
targetPeptideColumn.setCellValueFactory(cellData -> cellData.getValue().peptideProperty());
scoreColumn.setCellValueFactory(cellData -> cellData.getValue().scoreProperty().asObject());
rowColumn.setCellValueFactory(cellData -> cellData.getValue().rowProperty().asObject());
peptideNumColumn.setCellValueFactory(cellData -> cellData.getValue().peptideNumProperty().asObject());
dataTable.sort();
}
@FXML
private void initialize()
{
scoreColumn.setSortType(TableColumn.SortType.DESCENDING);;
// Initialize the data table with the five columns.
foundPeptideColumn.setCellValueFactory(cellData -> cellData.getValue().foundPeptideProperty());
targetPeptideColumn.setCellValueFactory(cellData -> cellData.getValue().peptideProperty());
scoreColumn.setCellValueFactory(cellData -> cellData.getValue().scoreProperty().asObject());
rowColumn.setCellValueFactory(cellData -> cellData.getValue().rowProperty().asObject());
peptideNumColumn.setCellValueFactory(cellData -> cellData.getValue().peptideNumProperty().asObject());
dataTable.getColumns().addAll(scoreColumn);
dataTable.getSortOrder().add(scoreColumn);
dataTable.sort();
}
dataTable.getColumns().addAll(scoreColumn);
dataTable.getSortOrder().add(scoreColumn);
dataTable.sort();