【发布时间】:2016-10-31 15:34:34
【问题描述】:
我有这个问题,当我运行我的应用程序时,我看不到元素比我之前添加到我的表中,我创建一个类(角色)并使用 PropertyValueFactory。感谢和抱歉语言错误,我说西班牙语。这是代码:
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);
}
}
这是 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;
}
}
【问题讨论】:
-
发布
Personas的代码。我想问题将是该类的缺失访问器。它应该有一个公共的nombreProperty()或getNombre()和一个公共的apellidoProperty()或getApellido(),返回类型为字符串。 -
好的,我发布代码,再次抱歉,我是这个页面的新手。
-
你认为我必须创建两个 getter 方法而不是返回名称(名词)和姓氏(apellido)吗?
标签: java javafx tableview elements