我在使用SceneBuilder的IntelliJ中使用JavaFX 8和JDK 1.8.0_77。我用SceneBuilder创建了Message Center GUI。我有一个包含五列的TableView, 开始日期 , 结束日期 , 消息< / em> , 类别 和 状态 。当单击 添加 按钮时,这些值将通过控件输入并保存在 消息对象 中。使用 msgTableView.getItems()将 消息 对象添加到 msgTableView 。 (消息)
问题是我在底行添加了一个消息对象,所有前面的行都改为最后一行;行数是正确的,但所有行都是相同的。
我的消息对象代码:
public class Message implements Serializable {
public static LocalDate startDate = LocalDate.now();
public static LocalDate endDate = LocalDate.now();
public static String newMsg;
public static String status;
public static String category;
public void Messages(LocalDate StartDate, LocalDate EndDate, String NewMsg, String Status, String Category){
this.startDate = StartDate;
this.endDate = EndDate;
this.newMsg = NewMsg;
this.status = Status;
this.category = Category;
}
// --------------Getters and Setters----------------------------
public LocalDate getStartDate() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
public LocalDate getEndDate() {
return endDate;
}
public void setEndDate(LocalDate endDate) {
this.endDate = endDate;
}
public String getNewMsg() {
return newMsg;
}
public void setNewMsg(String newMsg) {
this.newMsg = newMsg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCategory() {
return category;
}
public void setCategory(String type) {
this.category = type;
}
关键的控制器代码是:
// TableView --------------------------------------------------------------------------------
@FXML
private TableView<Message> msgTableView;
@FXML
private TableColumn<Message, LocalDate> StartDateTableCol;
@FXML
private TableColumn<Message, LocalDate> EndDateTableCol;
@FXML
private TableColumn<Message, String> MessageTableCol;
@FXML
private TableColumn<Message, String> CategoryTableCol;
@FXML
private TableColumn<Message, String> StatusTableCol;
@FXML
private DatePicker startDatePicker;
@FXML
private DatePicker endDatePicker;
@FXML
private TextField msgTextField;
@FXML
private ComboBox categoryComboBox;
@FXML
private Button addButton;
@FXML
private Button deleteButton;
@FXML
private Button editButton;
public void saveLine() {
Message message = new Message();
message.setStartDate(startDatePicker.getValue());
message.setEndDate(endDatePicker.getValue());
message.setNewMsg(msgTextField.getText());
message.setStatus("active");
message.setCategory(categoryComboBox.getValue().toString());
msgTableView.getItems().add(message);
clearLine();
}
public void clearLine() {
System.out.println( "\nEntered clearAll() " );
startDatePicker.setValue(null);
endDatePicker.setValue(null);
msgTextField.clear();
categoryComboBox.setValue("Category");
}