-1

I need to pass the date property to the setCellValueFactory , this is the code for the seters and geters of my Persona class, what is the correct method to do that, i think i having an issue whit the correct declaration, so i need a real direction here, i need to declare some date in the java.sql.date class, do some reference? any idea?. please a little help here.

   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);
}

}

Some of Controller here that set the values to the tableview

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();


}

this is the method i am using to retrieve the data in the tableview but the date does not show

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();
          }

     }

}

The date does not show in the tableview , i need to format the date to pass that value in the tableview i think. please some orientation here be helpful.

this is my FXML Code

<?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>
Jhon Mark
  • 3
  • 2
  • 7
  • unrelated to your problem: please learn java naming conventions and stick to them (hmm ... hadn't you been adviced to do so in your questions ;) – kleopatra Oct 19 '18 at 07:28
  • Possible duplicate of [Javafx tableview not showing data in all columns](https://stackoverflow.com/questions/18971109/javafx-tableview-not-showing-data-in-all-columns) – kleopatra Oct 20 '18 at 05:59
  • why dont you read the answer? it's perfect. – kleopatra Oct 20 '18 at 22:04

1 Answers1

5

You're using JavaFX properties incorrectly. A property for a JavaFX Bean needs a getter, setter (if writable), and a property getter. Also, the setter should not be creating a new property each time; it should be setting the value of the existing property.

To simplify your example, if we have a Person with a single property, name, it would look like this:

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;
    }

} 

Note: You can still initialize the properties in the constructor, instead of at the field declaration, if you want.

To use the name property in a TableView or TreeTableView you would simply return it in the cellValueFactory. Example using a TableView:

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

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

Another issue with your code is that you're using a raw ObjectProperty. Don't use raw types. Instead, you should be using an ObjectProperty<Date> where Date is java.sql.Date. For example:

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;
    }

}

Note: It'd probably be better to use one of the java.time classes (e.g. LocalDate, OffsetDateTime, etc...).

And again, to add it to a TableView it'd look like:

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

TableColumn<Person, Date> clientDateCol= new TableColumn<>("Client Date");
clientDateCol.setCellValueFactory(features -> features.getValue().clientDateProperty());
table.getColumns().add(clientDateCol);
Slaw
  • 37,820
  • 8
  • 53
  • 80
  • i did that but the tableview is empy when i send the date value, how get the right format?i think is that – Jhon Mark Oct 19 '18 at 15:13
  • @JhonMark [Edit](https://stackoverflow.com/posts/52885424/edit) your question to add a [mcve] that demonstrates the problem. – Slaw Oct 19 '18 at 15:48