2

I have this problem, when a run my application I Cant see the elements Than I before added to my table, I create a Class (Personas) and use the PropertyValueFactory. thanks and sorry for the error in the language, I speak Spanish. This is the code:

package ejemplo.tableview;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class EjemploTableView extends Application {

    @Override
    public void start(Stage primaryStage) {
        ObservableList<Personas> data = FXCollections.observableArrayList(
                new Personas("Diego","Maradona"),
                new Personas("Lionel","Messi")

        );
        TableView<Personas> tabla = new TableView();

        TableColumn<Personas,String> c1 = new TableColumn("Nombre");
        c1.setMinWidth(200d);
        c1.setCellValueFactory(new PropertyValueFactory<Personas,String>("nombre"));

        TableColumn<Personas,String> c2 = new TableColumn("Apellido");
        c2.setMinWidth(200d);
        c2.setCellValueFactory(new PropertyValueFactory<>("apellido"));

        tabla.getColumns().addAll(c1,c2);
        tabla.setItems(data);
        StackPane root = new StackPane();

        root.getChildren().add(tabla);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

this is the class Personas:

package ejemplo.tableview;

public class Personas {
    private String nombre;
    private String apellido;

    public Personas(String nombre,String apellido){
        this.nombre = nombre;
        this.apellido = apellido;

    }
}
  • 1
    Post the code of `Personas`. I guess the problem will be the missing accessors of that class. It shall have a public `nombreProperty()` or `getNombre()` and a public `apellidoProperty()` or `getApellido()` with String return type. – DVarga Jun 29 '16 at 05:30
  • Ok, I post the code, Sorry again, Im new in this page. – Santiago Bustamante Jun 29 '16 at 05:36
  • do you think than I have to create two getter methods than return the name(nombre) and the surname(apellido)? – Santiago Bustamante Jun 29 '16 at 05:42

1 Answers1

2

Check the documentation of PropertyValueFactory:

A convenience implementation of the Callback interface, designed specifically for use within the TableColumn cell value factory. An example of how to use this class is:

TableColumn firstNameCol = new TableColumn("First Name"); firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));

In this example, the "firstName" string is used as a reference to an assumed firstNameProperty() method in the Person class type (which is the class type of the TableView items list). Additionally, this method must return a Property instance. If a method meeting these requirements is found, then the TableCell is populated with this ObservableValue. In addition, the TableView will automatically add an observer to the returned value, such that any changes fired will be observed by the TableView, resulting in the cell immediately updating.

If no method matching this pattern exists, there is fall-through support for attempting to call get() or is() (that is, getFirstName() or isFirstName() in the example above). If a method matching this pattern exists, the value returned from this method is wrapped in a ReadOnlyObjectWrapper and returned to the TableCell. However, in this situation, this means that the TableCell will not be able to observe the ObservableValue for changes (as is the case in the first approach above).

Based on this the modified Personas class:

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Personas {
    private StringProperty nombre = new SimpleStringProperty();
    private StringProperty apellido = new SimpleStringProperty();

    public StringProperty nombreProperty() {return nombre;};
    public StringProperty apellidoProperty() {return apellido;};

    public Personas(String nombre, String apellido) {
        this.nombre.set(nombre);
        this.apellido.set(apellido);
    }

    public String getNombre() {return nombre.get();}
    public String getApellido() {return apellido.get();}
}
DVarga
  • 21,311
  • 6
  • 55
  • 60