Java FXML TableColumn不支持GregorianCalendar

Java FXML TableColumn not supporting GregorianCalendar

公历日历未正确显示在TableColumn的单元格中。当我使用弦、弦和双弦时,效果很好。当我运行应用程序时,包含日期的单元格显示

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000,dstSavings=3600000,..

但我希望它在年-月-日显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
public class ProjectenController implements Initializable {

  @
  FXML
  private TableView < Project > TVProject;@
  FXML
  private TableColumn < Project, Integer > TCID;@
  FXML
  private TableColumn < Project, String > TCNaam;@
  FXML
  private TableColumn < Project, GregorianCalendar > TCBeginDatum;@
  FXML
  private TableColumn < Project, GregorianCalendar > TCEindDatum;@
  FXML
  private TableColumn < Project, Double > TCAanneemsom;@
  FXML
  private TableColumn < Project, String > TCGefactureerd;@
  FXML
  private TableColumn < Project, String > TCBeschrijving;

  final ObservableList < Project > data = FXCollections.observableArrayList(
    new Project(1,"500", new GregorianCalendar(2015, 1, 23), null, 1000, false, null)
  );

  @
  Override
  public void initialize(URL url, ResourceBundle rb) {
    TCID.setCellValueFactory(new PropertyValueFactory < > ("ProjectNr"));
    TCNaam.setCellValueFactory(new PropertyValueFactory < > ("Naam"));
    TCBeginDatum.setCellValueFactory(new PropertyValueFactory < > ("BDatum"));
    TCEindDatum.setCellValueFactory(new PropertyValueFactory < > ("EDatum"));
    TCAanneemsom.setCellValueFactory(new PropertyValueFactory < > ("Aanneemsom"));
    TCGefactureerd.setCellValueFactory(new PropertyValueFactory < > ("Gefactureerd"));
    TCBeschrijving.setCellValueFactory(new PropertyValueFactory < > ("Beschrijving"));
    TVProject.setItems(data);

  }
}



public class Project {

  private int ProjectNr;
  private String Naam;
  private GregorianCalendar BDatum;
  private GregorianCalendar EDatum;
  double Aanneemsom;
  boolean Gefactureerd;
  String Beschrijving;
  List < Artikel > Artikelen;
  List < Activiteit > Activiteiten;

  public Project(int projectnr, String naam, GregorianCalendar bdatum,
    GregorianCalendar edatum, double aansom, boolean fact, String besc) {
    this.ProjectNr = projectnr;
    this.Naam = naam;
    this.BDatum = bdatum;
    this.EDatum = edatum;
    this.Aanneemsom = aansom;
    this.Gefactureerd = fact;
    this.Beschrijving = besc;
  }

  public int getProjectNr() {
    return this.ProjectNr;
  }

  public String getNaam() {
    return this.Naam;
  }

  public GregorianCalendar getBDatum() {
    return this.BDatum;
  }

  public GregorianCalendar getEDatum() {
    return this.EDatum;
  }

  public double getAanneemsom() {
    return this.Aanneemsom;
  }

  public String getFactureerd() {
    if (this.Gefactureerd == true) {
      return"Ja";
    } else {
      return"Nee";
    }
  }

  public String getBeschrijving() {
    return this.Beschrijving;
  }
}

创建返回带格式化日期的字符串的方法。在控制器中:

1
2
3
4
@FXML
private TableColumn < Project, String> TCBeginDatum;
...
TCBeginDatum.setCellValueFactory(new PropertyValueFactory < > ("formattedBDatum"));

在你的豆子里:

1
2
3
public String getFormattedBDatum() {
  return this.BDatum!=null ? new SimpleDateFormat("yyyy/MM/dd").format(this.BDatum.getTime()) :"";
}