【问题标题】:JavaFX sort TableView column by date (dd/mm/yyyy format)JavaFX 按日期对 TableView 列进行排序(dd/mm/yyyy 格式)
【发布时间】:2017-10-06 15:58:17
【问题描述】:

我有一个包含许多列的 TableView。第一个包含日期(dd/mm/yyyy 格式),但日期没有“正确”排序。

这是我一直在准备的一个小例子:

这里是代码:

Test.java

package test;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Test extends Application {
    private AnchorPane rootLayout;

    @Override
    public void start(Stage primaryStage) throws Exception{

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Test.class.getResource("FXMLDocument.fxml"));
        FXMLDocumentController controller = new FXMLDocumentController();
        loader.setController(controller);

        rootLayout = (AnchorPane) loader.load();
        Scene scene = new Scene(rootLayout);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Test");

        primaryStage.centerOnScreen();
        primaryStage.show();

    }

}

FXMLDocumentController.java

package test;

import java.util.List;
import java.util.Map;
import javafx.fxml.FXML;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class FXMLDocumentController {
    private ObservableList<ExampleTable> datos;
    @FXML public TableView<ExampleTable> tv_example;
    @FXML public TableColumn<ExampleTable, String> col_date, col_name;

    public void initialize(){
        datos = FXCollections.observableArrayList();
        col_date.setCellValueFactory(new PropertyValueFactory<>("date"));
        col_name.setCellValueFactory(new PropertyValueFactory<>("name"));

        setTable();
    }

    public void setTable(){
        List<Map<String, String>> lmap = ExampleTable.lmap();
        for(Map<String, String> element : lmap){
            ExampleTable objEt = new ExampleTable(element.get("date"), element.get("name"));
            datos.add(objEt);
        }

        tv_example.setItems(datos);
        tv_example.getSortOrder().setAll(col_date);
    }
}

ExampleTable.java

package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class ExampleTable {
    public StringProperty date;
    public StringProperty name;

    public ExampleTable(String date, String name){
        this.date = new SimpleStringProperty(date);
        this.name = new SimpleStringProperty(name);
    }

    public String getDate(){
        return date.get();
    }

    public void setDate(String date){
        this.date.set(date);
    }

    public String getName(){
        return name.get();
    }

    public void setName(String name){
        this.name.set(name);
    }

    public static List<Map<String, String>> lmap(){
        List<Map<String, String>> list = new ArrayList<>();

        Map<String, String> row = new HashMap<>();
        row.put("date", "14/01/1988");
        row.put("name", "Hagen");
        list.add(row);

        row = new HashMap<>();
        row.put("date", "27/02/1988");
        row.put("name", "Herrman");
        list.add(row);

        row = new HashMap<>();
        row.put("date", "16/03/1988");
        row.put("name", "Hank");
        list.add(row);

        row = new HashMap<>();
        row.put("date", "19/05/1994");
        row.put("name", "Jack");
        list.add(row);

        return list;
    }
}

以及 FXML 文件:

FXMLDocument.fxml

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

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
   <children>
      <VBox layoutX="32.0" layoutY="40.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="4.0" AnchorPane.leftAnchor="4.0" AnchorPane.rightAnchor="4.0" AnchorPane.topAnchor="4.0">
         <children>
            <TableView fx:id="tv_example" maxHeight="1.7976931348623157E308" prefWidth="200.0" VBox.vgrow="ALWAYS">
              <columns>
                <TableColumn fx:id="col_date" minWidth="120.0" prefWidth="120.0" text="Date" />
                <TableColumn fx:id="col_name" minWidth="100.0" prefWidth="300.0" text="Name" />
              </columns>
            </TableView>
         </children>
      </VBox>
   </children>
</AnchorPane>

编辑:

我刚刚使用 LocalDate 而不是 String 作为日期进行了这些更改:

ExampleTable.java

public ObjectProperty<LocalDate> date;
    public StringProperty name;

    public ExampleTable(LocalDate date, String name){
        this.date = new SimpleObjectProperty(date);
        this.name = new SimpleStringProperty(name);
    }

    public LocalDate getDate(){
        return date.get();
    }

    public void setDate(LocalDate date){
        this.date.set(date);
    }

FXMLDocumentController.java

public class FXMLDocumentController {
    private ObservableList<ExampleTable> datos;
    @FXML public TableView<ExampleTable> tv_example;
    @FXML public TableColumn<ExampleTable, LocalDate> col_date;
    @FXML public TableColumn<ExampleTable, String> col_name;

    public void initialize(){
        datos = FXCollections.observableArrayList();
        col_date.setCellValueFactory(new PropertyValueFactory<>("date"));
        col_name.setCellValueFactory(new PropertyValueFactory<>("name"));

        setTable();
    }

    public void setTable(){
        List<Map<String, String>> lmap = ExampleTable.lmap();

        for(Map<String, String> element : lmap){
            LocalDate dt = ExampleTable.toLocalDate(element.get("date"));
            ExampleTable objEt = new ExampleTable(dt, element.get("name"));
            datos.add(objEt);
        }

        tv_example.setItems(datos);
        tv_example.getSortOrder().setAll(col_date);
    }
}

这是ExampleTable中的toLocalDate方法:

public static LocalDate toLocalDate(String date){
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate dt = LocalDate.parse(date, formatter);
        return dt;
    }

我得到了这个:

【问题讨论】:

  • LocalDate列的数据类型改为String(即使用TableColumn&lt;ExampleTable, LocalDate&gt; colDate,将ExampleTable中的date类型改为ObjectProperty&lt;LocalDate&gt;等)。
  • 按照您的建议,我已将其更改为“LocalDate”,但现在日期为 yyy-mm-dd 格式,如图 02 所示。
  • 查看答案。 FWIW 我会尽可能地传播从StringLocalDate 的更改,即将您的static 方法更改为只返回List&lt;ExampleTable&gt; 而不是地图。你应该总是LocalDate而不是String来表示日期,除非你想显示它们(这是在实际单元格的实现中)。

标签: sorting date javafx format tableview


【解决方案1】:

您可以使用cellFactory 更改LocalDate 在表格单元格中的显示方式(除了您已有的cellValueFactory):

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
col_date.setCellFactory(tc -> new TableCell<ExampleTable, LocalDate>() {
    @Override
    protected void updateItem(LocalDate date, boolean empty) {
        super.updateItem(date, empty);
        if (empty) {
            setText(null);
        } else {
            setText(formatter.format(date));
        }
    }
});

【讨论】:

  • 这样的答案应该有赞成票。来,拿第一个 :)
  • 我花了 2 天时间才找到这个漂亮的答案。
猜你喜欢
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
相关资源
最近更新 更多