如何为tableview声明日期值属性

-1 javafx

我需要将日期属性传递给 setCellValueFactory ,这是我的 seters 和 geters 的代码Persona class,正确的方法是什么,我认为我在正确的声明中遇到了问题,所以我在这里需要一个真正的方向,我需要在java.sql.date类中声明一些日期,可以参考一下吗?任何想法?。请在这里提供一点帮助。

   package application;


import java.time.LocalDate;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Persona {



    private StringProperty nombres;
    private StringProperty apellidos;
    private IntegerProperty id_cliente;
    private ObjectProperty <LocalDate>fechacliente;

public Persona (   String nombres, String apellidos, Integer id_cliente, Object fechacliente) {
    this.nombres=  new SimpleStringProperty (nombres);
    this.apellidos= new SimpleStringProperty ( apellidos);
    this.id_cliente=new SimpleIntegerProperty (id_cliente);
    this.fechacliente= new SimpleObjectProperty<LocalDate>();
}


public Object getFecha() {
    return fechacliente.get();
}

public void setFecha(Object fechacliente) {
    this.fechacliente=new SimpleObjectProperty<>();

}

public String getNombres() {
    return nombres.get();
}

public  void  setNombres(String nombres) {
    this.nombres=new SimpleStringProperty (nombres);
}


public String getApellidos() {
    return apellidos.get();
}

public  void  setApellidos(String apellidos) {
    this.apellidos=new SimpleStringProperty ( apellidos);
}


public Integer getId_cliente() {
    return id_cliente.get();
}

public  void  setid_cliente(Integer id_cliente) {
    this.id_cliente=new SimpleIntegerProperty (id_cliente);
}

}
Run Code Online (Sandbox Code Playgroud)

其中一些Controller将值设置为表视图

public void initialize(URL arg0, ResourceBundle arg1) {
            clienteid.setCellValueFactory(new PropertyValueFactory <Persona, Integer>("id_cliente"));
            nombrescol.setCellValueFactory(new PropertyValueFactory <Persona, String>("nombres"));
             apellidoscol.setCellValueFactory(new PropertyValueFactory <Persona, String>("apellidos"));
             fechacli.setCellValueFactory(new PropertyValueFactory <Persona, LocalDate>("fechacliente"));

 seleccionaregistros();
     seleccionanombre();
     seleccionapellido();


}
Run Code Online (Sandbox Code Playgroud)

这是我用来检索表视图中的数据的方法,但日期不显示

public void seleccionaregistros() {
    ObservableList <Persona> data =FXCollections.observableArrayList();
      Connection conn=null;{
          try {

             conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
              Statement mostrar=conn.createStatement();
              ResultSet rs;
              rs= mostrar.executeQuery("select * from cliente");


              while ( rs.next() ) 
              {
                 data.add(new Persona(

                         rs.getString("nombre"),
                         rs.getString("apellido"),
                         rs.getInt("id"),
                         rs.getDate(4)
                         ));
                 tablacliente.setItems(data);
              }

          } catch (SQLException e) {
              e.printStackTrace();
          }

     }

}
Run Code Online (Sandbox Code Playgroud)

日期没有显示在表视图中,我认为我需要格式化日期以在表视图中传递该值。请在这里提供一些指导会有所帮助。

这是我的FXML代码

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="497.0" prefWidth="943.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConexionController">
   <children>
      <Pane layoutY="-3.0" prefHeight="605.0" prefWidth="1084.0">
         <children>
            <Button fx:id="btn" layoutX="145.0" layoutY="109.0" mnemonicParsing="false" onAction="#cargarconexion" prefHeight="46.0" prefWidth="117.0" text="Prueba Conexion" />
            <Button fx:id="mtn" layoutX="15.0" layoutY="183.0" mnemonicParsing="false" onAction="#cargarregistro" prefHeight="46.0" prefWidth="117.0" text="Inserta Registro" />
            <Label layoutX="14.0" layoutY="279.0" prefHeight="17.0" prefWidth="105.0" text="NOMBRES" />
            <Label layoutX="15.0" layoutY="327.0" prefHeight="17.0" prefWidth="79.0" text="APELLIDOS" />
            <TextField fx:id="nm" layoutX="159.0" layoutY="275.0" prefHeight="25.0" prefWidth="149.0" />
            <TextField fx:id="ap" layoutX="159.0" layoutY="323.0" />
            <Button fx:id="lmp" layoutX="159.0" layoutY="484.0" mnemonicParsing="false" onAction="#borrarcasillatexto" prefHeight="25.0" prefWidth="150.0" text="Limpiar Texto" />
            <TableView fx:id="tablacliente" layoutX="355.0" layoutY="15.0" prefHeight="383.0" prefWidth="696.0">
              <columns>
                <TableColumn fx:id="clienteid" prefWidth="159.0" text="ID" />
                <TableColumn fx:id="nombrescol" prefWidth="159.0" text="NOMBRES" />
                  <TableColumn fx:id="apellidoscol" minWidth="0.0" prefWidth="169.0" text="APELLIDOS" />
                  <TableColumn fx:id="fechacli" prefWidth="235.0" text="FECHA DE NACIMIENTO" />
              </columns>
            </TableView>
            <Button fx:id="mts" layoutX="15.0" layoutY="109.0" mnemonicParsing="false" onAction="#mostrartodo" prefHeight="46.0" prefWidth="117.0" text="Mostrar" />
            <TextField fx:id="bq" layoutX="554.0" layoutY="417.0" prefHeight="25.0" prefWidth="149.0" />
            <Button fx:id="bqd" layoutX="758.0" layoutY="417.0" mnemonicParsing="false" onAction="#buscanm" prefHeight="25.0" prefWidth="155.0" text="BUSCAR NOMBRE" />
            <Button fx:id="bqape" layoutX="758.0" layoutY="458.0" mnemonicParsing="false" onAction="#buscaape" prefHeight="25.0" prefWidth="155.0" text="BUSCAR POR APELLIDO" />
            <TextField fx:id="bqa" layoutX="554.0" layoutY="458.0" />
            <ComboBox layoutX="159.0" layoutY="430.0" prefWidth="150.0" />
            <Label layoutX="15.0" layoutY="434.0" prefHeight="17.0" prefWidth="55.0" text="GENERO" />
            <MenuBar fx:id="menucombo" layoutY="3.0">
              <menus>
                <Menu mnemonicParsing="false" text="Agregar">
                  <items>
                    <MenuItem mnemonicParsing="false" onAction="#inicializacombo" text="Datos Cliente" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
            <Button fx:id="botonborrar" layoutX="758.0" layoutY="507.0" mnemonicParsing="false" onAction="#borraregistroid" prefHeight="25.0" prefWidth="155.0" text="BORRAR REGISTRO" />
            <TextField fx:id="borrar" layoutX="554.0" layoutY="507.0" />
            <DatePicker fx:id="mifecha" layoutX="158.0" layoutY="371.0" prefHeight="25.0" prefWidth="150.0" />
            <Label layoutX="15.0" layoutY="375.0" prefHeight="17.0" prefWidth="150.0" text="FECHA DE NACIMIENTO" />
         </children>
      </Pane>
   </children>
</AnchorPane>
Run Code Online (Sandbox Code Playgroud)

Sla*_*law 5

您错误地使用了 JavaFX 属性。JavaFX Bean 的属性需要一个 getter、setter(如果可写)和一个属性 getter。另外,setter 不应该每次都创建一个新的属性;它应该设置现有属性的值。

为了简化您的示例,如果我们有一个Person具有单个属性的 ,name它看起来像这样:

public class Person {

    private final StringProperty name = new SimpleStringProperty(this, "name");

    public Person(String name) {
        setName(name);
    }

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

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

    public final StringProperty nameProperty() { // property getter
        return name;
    }

} 
Run Code Online (Sandbox Code Playgroud)

注意:如果需要,您仍然可以在构造函数中初始化属性,而不是在字段声明中初始化。

name在 aTableView或中使用该属性TreeTableView,您只需将其返回到cellValueFactory. 使用示例TableView

TableView<Person> table = new TableView<>();

TableColumn<Person, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(features -> features.getValue().nameProperty());
table.getColumns().add(nameCol);
Run Code Online (Sandbox Code Playgroud)

您的代码的另一个问题是您使用的是原始 ObjectProperty. 不要使用原始类型。相反,您应该使用ObjectProperty<Date>where Dateis java.sql.Date。例如:

public class Person {

    private final ObjectProperty<Date> clientDate = new SimpleObjectProperty<>(this, "clientDate");

    public final void setClientDate(Date clientDate) {
        this.clientDate.set(clientDate);
    }

    public final Date getClientDate() {
        return clientDate.get();
    }

    public final ObjectProperty<Date> clientDateProperty() {
        return clientDate;
    }

}
Run Code Online (Sandbox Code Playgroud)

注意:使用其中一个类可能会更好java.time(例如LocalDateOffsetDateTime等...)。

再次,将其添加到 a 中,TableView如下所示:

TableView<Person> table = new TableView<>();

TableColumn<Person, Date> clientDateCol= new TableColumn<>("Client Date");
clientDateCol.setCellValueFactory(features -> features.getValue().clientDateProperty());
table.getColumns().add(clientDateCol);
Run Code Online (Sandbox Code Playgroud)