JavaFX:默认情况下按特定列对TableView进行排序

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

该表的记录都是同时加载的,并且不可更新,因此我不必担心顺序更改,因此排序只需要执行一次。

这是使用数据以及tableView和TableColumn声明初始化dataTable的方法:

...

@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自动对表格进行排序的方法吗?

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

asdz321 回答:JavaFX:默认情况下按特定列对TableView进行排序

您是否使用SortedList作为表项的基础? https://openjfx.io/javadoc/11/javafx.base/javafx/collections/transformation/SortedList.html

请参阅https://openjfx.io/javadoc/11/javafx.controls/javafx/scene/control/TableView.html的“排序”部分

,

(基于这个问题的答案:JavaFX: TableView: Arrow for 默认排序的列)

我遇到了同样的问题,我使用了链接的答案。

您必须在将数据填入表格后插入这部分代码。

dataTable.getColumns().addAll(scoreColumn);
dataTable.getSortOrder().add(scoreColumn);
dataTable.sort();

它对我有用。

本文链接:https://www.f2er.com/1288715.html

大家都在问