首页 > 解决方案 > JavaFX:默认情况下按特定列对 TableView 进行排序

问题描述

我有一个 TableView,dataTable,它包含五列。我想根据 Integer 类型的 TableColumn scoreColumn 对行进行排序。用户可以通过单击 scoreColumn 标题的箭头来执行此操作,但我希望默认情况下按降序对数据进行排序。

表的记录都是同时加载的,不可更新,所以不用担心顺序变化,排序只需要执行一次。

下面是使用数据初始化 dataTable 的方法,以及 TableView 和 TableColumn 声明:

...

@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());
}

...

...这是具有 TableView 的 FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.address.view.AppOverviewController">
   <children>
      <TableView fx:id="dataTable" layoutX="556.0" layoutY="192.0" prefHeight="700.0" prefWidth="1000.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="50.0">
        <columns>
          <TableColumn fx:id="foundPeptideColumn" maxWidth="-1.0" minWidth="275.0" prefWidth="275.0" text="Found Peptide" />
          <TableColumn fx:id="targetPeptideColumn" maxWidth="-1.0" minWidth="275.0" prefWidth="275.0" text="Target Peptide" />
            <TableColumn fx:id="scoreColumn" maxWidth="-1.0" minWidth="150.0" prefWidth="150.0" sortType="DESCENDING" text="Score" />
            <TableColumn fx:id="rowColumn" maxWidth="-1.0" minWidth="150.0" prefWidth="150.0" text="Row" />
            <TableColumn fx:id="peptideNumColumn" maxWidth="-1.0" minWidth="150.0" prefWidth="150.0" text="Peptide #" />
        </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
      <TextField fx:id="peptideField" layoutX="113.0" layoutY="14.0" />
      <Label layoutX="14.0" layoutY="19.0" text="Enter peptide:" />
      <Button layoutX="294.0" layoutY="14.0" mnemonicParsing="false" onAction="#handleSearch" text="Search" />
      <Button layoutX="370.0" layoutY="14.0" mnemonicParsing="false" onAction="#handleClearTable" text="Clear Table" />
   </children>
</AnchorPane>

我尝试过的事情:

@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();
}

(基于这个问题的答案:Sort TableView by certain column Javafx

@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();
}

(基于这个问题的答案:JavaFX: TableView: Arrow for a column sorted by default

我还尝试了一些与上述示例类似的其他事情(主要是使用上述尝试修复的放置/顺序/组合的组合),但都无济于事。

我还应该尝试以编程方式、在 FXML 中还是在 SceneBuilder 中根据 scoreColumn 自动对表进行排序?

我可以用我的应用程序的图像更新我的帖子,或者为我的控制器添加额外的代码,如果这有帮助的话。

标签: javajavafxtableviewfxmlscenebuilder

解决方案



推荐阅读