如何解决TableView列中未显示日期数据
我正在用JavaFX制作一个小项目,该项目将人员及其各自的食品订单添加到列表中,到目前为止,除了上述列表中的“ fecha”列外,它的工作都令人惊奇。应该可以接收到接受订单后的当前本地日期和时间,但不能接受。
这是主要班级:
public class App extends Application {
private static Scene scene;
private static Stage stageAux; // Copia auxiliar de Stage,para uso en métodos menores
@Override
public void start(Stage stage) throws IOException {
stageAux = stage;
scene = new Scene(loadFXML("primary"),1200,720);
stage.setScene(scene);
stage.setTitle("Comandas Desayuno");
stage.show();
}
static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml));
}
private static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
return fxmlLoader.load();
}
public static void main(String[] args) {
launch();
}
public boolean fullscreen(){
stageAux.setFullScreen(!stageAux.isFullScreen());
return stageAux.isFullScreen();
}
}
这是主控制器:
public class PrimaryController implements Initializable {
private Connection con;
private Statement stat;
private ObservableList<Pedidos> pedidosList;
private Pedidos pedidoSeleccionado;
private Integer seleccion;
private int id = 1; // Contador de id de cada cliente
// SimpleDateFormat y Date para coger la hora a la que se ha hecho el pedido
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date currentDate = new Date();
private App app = new App();
private Functions f1 = new Functions();
@FXML
private MenuItem menuFileClose;
@FXML
private TableView<Pedidos> tablaId;
@FXML
private TableColumn<Pedidos,Integer> idColumn;
@FXML
private TableColumn<Pedidos,String> bebidaColumn;
@FXML
private TableColumn<Pedidos,String> comidaColumn;
@FXML
private TableColumn<Pedidos,String> clienteColumn;
@FXML
private TableColumn<Pedidos,Double> precioColumn;
@FXML
private TableColumn<Pedidos,Date> fechaColumn;
@FXML
private TableColumn<Pedidos,Boolean> entregaColumn1;
@FXML
private MenuItem fullscreenItem;
@FXML
private TextField clientName;
@FXML
private ChoiceBox<String> comidaBox;
@FXML
private ChoiceBox<String> bebidaBox;
@FXML
private Button saveButton;
@FXML
private Button deleteButton;
@FXML
private CheckBox entregadoCheck;
@FXML
private Label elementoLabel;
@FXML
private Button controlPanelButton;
private void clearElement(){
clientName.setText("");
bebidaBox.getSelectionModel().select("Nada");
comidaBox.getSelectionModel().select("Nada");
entregadoCheck.setSelected(false);
}
private void editData(){
elementoLabel.setText("Editar Elemento");
f1.resetPrecioPedido();
f1.setBebida(bebidaBox.getValue());
f1.setComida(comidaBox.getValue());
pedidoSeleccionado = new Pedidos(
pedidoSeleccionado.getId(),bebidaBox.getValue(),comidaBox.getValue(),clientName.getText(),f1.getPrecioPedido(),currentDate,entregadoCheck.isSelected()
);
pedidosList.set(seleccion,pedidoSeleccionado);
tablaId.refresh(); // Actualiza la tabla automáticamente
tablaId.getSelectionModel().select(null); // Deselecciona la fila que se acaba de editar
elementoLabel.setText("Nuevo Elemento");
}
// Guarda los pedidos nuevos / Actualiza los pedidos seleccionados(con editData())
@FXML
private void save(ActionEvent event) {
f1.setBebida(bebidaBox.getValue());
f1.setComida(comidaBox.getValue());
Pedidos p = new Pedidos(
id,entregadoCheck.isSelected()
);
if(pedidoSeleccionado == null){
if(!pedidosList.contains(p)){
pedidosList.add(p);
id++;
}
} else {
editData();
}
f1.resetPrecioPedido();
clearElement();
}
// Pone en pantalla los datos editables del pedido seleccionado
@FXML
private void seleccionar(MouseEvent event) {
pedidoSeleccionado = tablaId.getSelectionModel().getSelectedItem();
seleccion = tablaId.getSelectionModel().getSelectedIndex();
clientName.setText(pedidoSeleccionado.getCliente());
bebidaBox.getSelectionModel().select(pedidoSeleccionado.getBebida());
comidaBox.getSelectionModel().select(pedidoSeleccionado.getComida());
entregadoCheck.setSelected(pedidoSeleccionado.getEntregado());
}
@FXML
private void delete(ActionEvent event) {
Pedidos p = tablaId.getSelectionModel().getSelectedItem();
pedidosList.remove(p);
}
@FXML
private void close(ActionEvent event) {
System.exit(0);
}
@FXML
private void goFullscreen(ActionEvent event) {
app.fullscreen();
}
@Override
public void initialize(URL url,ResourceBundle rb){
pedidosList = FXCollections.observableArrayList();
tablaId.setItems(pedidosList);
idColumn.setCellValueFactory(new PropertyValueFactory("id"));
clienteColumn.setCellValueFactory(new PropertyValueFactory("cliente"));
bebidaColumn.setCellValueFactory(new PropertyValueFactory("bebida"));
comidaColumn.setCellValueFactory(new PropertyValueFactory("comida"));
precioColumn.setCellValueFactory(new PropertyValueFactory("precio"));
fechaColumn.setCellValueFactory(new PropertyValueFactory("fecha"));
entregaColumn1.setCellValueFactory(new PropertyValueFactory("entregado"));
// Lista completa de bebidas
bebidaBox.getItems().addAll(
"Nada","Café Solo - 0.8€","Café con Leche - 1€","Refresco - 1.5€","Colacao - 1.5€");
bebidaBox.getSelectionModel().select("Nada");
// Lista completa de comidas
comidaBox.getItems().addAll(
"Nada","Pitufo Mixto - 1.5€","Pitufo Bacon - 1.7€","Pitufo Lomo - 2€","Bocata Paté - 2.5€","Bocata Bacon - 3€","Bocata Chorizo - 3.5€");
comidaBox.getSelectionModel().select("Nada");
}
}
这是“ Pedidos”类:
public class Pedidos {
private int id;
private String bebida;
private String comida;
private String cliente;
private double precio;
private Date currentDate;
private Boolean entregado;
public Pedidos() {
}
public Pedidos(int id,String bebida,String comida,String cliente,double precio,Date currentDate,boolean entregado) {
this.id = id;
this.bebida = bebida;
this.comida = comida;
this.cliente = cliente;
this.precio = precio;
this.currentDate = currentDate;
this.entregado = entregado;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBebida() {
return bebida;
}
public void setBebida(String bebida) {
this.bebida = bebida;
}
public String getComida() {
return comida;
}
public void setComida(String comida) {
this.comida = comida;
}
public String getCliente() {
return cliente;
}
public void setCliente(String cliente) {
this.cliente = cliente;
}
public double getPrecio() {
return precio;
}
public void setPrecio(double precio) {
this.precio = precio;
}
public Date getCurrentDate() {
return currentDate;
}
public void setCurrentDate(Date currentDate) {
this.currentDate = currentDate;
}
public Boolean getEntregado() {
return entregado;
}
public void setEntregado(Boolean entregado) {
this.entregado = entregado;
}
@Override
public int hashCode() {
int hash = 7;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pedidos other = (Pedidos) obj;
if (this.id != other.id) {
return false;
}
if (Double.doubleToLongBits(this.precio) != Double.doubleToLongBits(other.precio)) {
return false;
}
if (!Objects.equals(this.bebida,other.bebida)) {
return false;
}
if (!Objects.equals(this.comida,other.comida)) {
return false;
}
if (!Objects.equals(this.cliente,other.cliente)) {
return false;
}
if (!Objects.equals(this.currentDate,other.currentDate)) {
return false;
}
if (!Objects.equals(this.entregado,other.entregado)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Pedidos{" + "id=" + id + ",bebida=" + bebida + ",comida=" + comida + ",cliente=" + cliente + ",precio=" + precio + ",currentDate=" + currentDate + ",entregado=" + entregado + '}';
}
}
这是当我在列表中输入新人员时出现的终端输出:
WARNING: Can not retrieve property 'fecha' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@6f86016e with provided class type: class modelos.Pedidos
java.lang.IllegalStateException: Cannot read from unreadable property fecha
at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:170)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:184)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:154)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:133)
at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:593)
at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
at javafx.controls/javafx.scene.control.TableCell.updateItem(TableCell.java:646)
at javafx.controls/javafx.scene.control.TableCell.indexChanged(TableCell.java:469)
at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:539)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.<init>(TableRowSkinBase.java:159)
at javafx.controls/javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:89)
at javafx.controls/javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:213)
at javafx.controls/javafx.scene.control.Control.doProcessCSS(Control.java:897)
at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:145)
at javafx.graphics/javafx.scene.Node.processCSS(Node.java:9540)
at javafx.graphics/javafx.scene.Node.applyCss(Node.java:9627)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1749)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1726)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1852)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2755)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1206)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
at javafx.graphics/javafx.scene.Scene.doLayoutPass(Scene.java:576)
at javafx.graphics/javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2482)
at javafx.graphics/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:412)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:411)
at javafx.graphics/com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:438)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:563)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:543)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:536)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:342)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
这可能真的很愚蠢,但是我已经花了一段时间了,但我只是找不到如何使它起作用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。