2016-06-29 25 views
2

我有这个问题,当我运行我的应用程序我看不到元素比我添加到我的表之前,我创建一个类(Personas)和使用PropertyValueFactory。感谢和抱歉在语言错误,我会说西班牙语。这是代码:我不能看到TableView的元素 - JavaFX

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

} 

这是类人物角色:

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

后Personas'的'代码。我想这个问题会成为那个班的缺失访问者。它应该有一个带有String返回类型的公共'nombreProperty()'或'getNombre()'和一个公共的'apellidoProperty()'或'getApellido()'。 – DVarga

+0

好的,我发布了代码,对不起,在这个页面中我是新的。 –

+0

你觉得比我必须创建两个getter方法比返回名称(nombre)和姓氏(apellido)? –

回答

2

检查的PropertyValueFactory文档:

的便捷实现回调接口,设计用 专在TableColumn单元格值工厂中。 如何使用此类的示例如下:

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

在这个例子中,“名字”的字符串被用作到 参考假定firstNameProperty()方法在 Person类型(这是 类类型的的TableView项目列表的)。此外,此方法 必须返回属性实例。如果找到符合这些 要求的方法,则TableCell将填充此ObservableValue。此外,TableView会自动将 观察者添加到返回值中,以便TableView观察到的任何更改触发为 ,从而导致单元立即更新。

如果没有方法匹配该模式的存在,对于试图呼叫得到()是()(即 是,的getFirstName()或isFirstName()在上面的例子)落空 支持或。如果存在匹配此模式的方法 ,则从此方法返回的值为 ,该值包装在ReadOnlyObjectWrapper中并返回到TableCell。然而,在这种情况下,这意味着TableCell不会是 能够观察ObservableValue的变化(如上面的第一种方法 )。

在此基础上修改Personas类:

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