Sorting JavaFX按日期排序TableView列(dd/mm/yyyy格式)
我有一个包含许多列的TableView。第一个包含日期(格式为Sorting JavaFX按日期排序TableView列(dd/mm/yyyy格式),sorting,date,javafx,format,tableview,Sorting,Date,Javafx,Format,Tableview,我有一个包含许多列的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 ja
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();
}
}
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);
}
}
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;
}
}
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);
}
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);
}
}
FXMLDocumentController.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();
}
}
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);
}
}
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;
}
}
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);
}
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);
}
}
我明白了:
您可以使用
cellFactory
更改LocalDate
在表格单元格中的显示方式(除了您已有的cellValueFactory
之外):
DateTimeFormatter formatter=DateTimeFormatter.of模式(“dd/MM/yyyy”);
col_date.setCellFactory(tc->new TableCell()){
@凌驾
受保护的void updateItem(LocalDate,布尔值为空){
super.updateItem(日期,空);
if(空){
setText(空);
}否则{
setText(formatter.format(date));
}
}
});
将列LocalDate
的数据类型改为String
(即使用TableColumn colDate
,并将ExampleTable
中的date
的类型更改为ObjectProperty
),如您所建议,我已将其更改为“LocalDate”,但现在日期为yyy-mm-dd格式,如图02所示,请参阅答案。FWIW我会尽可能地将更改从String
传播到LocalDate
,也就是说,将静态方法更改为只返回列表而不是映射。基本上,您应该始终使用LocalDate
表示日期,而不是String
,除非您希望显示日期(这是在实际单元格的实现中)。这样的答案应该有向上投票。我花了两天时间才找到这个漂亮的答案。