如何解决javafx上的TableView显示空行
我想创建一个方法,该方法将在BorderPane中返回一个表 ,但是我得到了空表,并且它的标签显示在GUI上。 这是代码:
@Override
public void start(Stage stage) throws Exception {
BorderPane border = new BorderPane();
VBox cbox = addCbox(); //center box
border.setCenter(cbox);
Scene scene = new Scene(border,600,650);
stage.setTitle("Test Project"); // Set the stage title
stage.setScene(scene); // Place the scene in the stage
stage.show(); // Display the stage
}
这是我通过遵循文档创建表数据和方法的方式:
private final TableView<Doctor> table = new TableView<>();
private final ObservableList<Doctor> data = FXCollections.observableArrayList(
new Doctor("196","Daniel LW","Cardiologists","9-6","MBBCh"),new Doctor("197","James DN","Anesthesiologists","4-9","LRCP"),new Doctor("199","Chales AK","Dermatologists","6-12","BSc")
);
public static void main(String[] args) {
launch(args);
}
public VBox addCbox() {
final Label label = new Label("Doctor List");
label.setFont(new Font("Arial",20));
table.setEditable(true);
TableColumn<Doctor,String> col1 = new TableColumn<Doctor,String>("ID");
col1.setMinWidth(30);
col1.setMaxWidth(60);
col1.setCellValueFactory(new PropertyValueFactory<>("id"));
TableColumn<Doctor,String> col2 = new TableColumn<Doctor,String>("NAME");
col2.setMinWidth(100);
col2.setCellValueFactory(new PropertyValueFactory<>("name"));
table.setItems(data);
table.getColumns().addAll(col1,col2);
// the above line prompt me warning message,but although I use:
// table.getColumns().add(col1); table.getColumns().add(col2);
// the rows of data still not visible
// I only get the empty clickable rows
final VBox cbox = new VBox();
cbox.setPadding(new Insets(10,20,10,10));
cbox.getChildren().addAll(label,table);
return cbox;
}
这是我创建的类的简化版本。有设置者和获取者。
public static class Doctor {
private SimpleStringProperty id,name,specialist,workTime,qualification;
public Doctor(String id,String name,String specialist,String workTime,String qualification) {
this.id = new SimpleStringProperty(id);
this.name = new SimpleStringProperty(name);
this.specialist = new SimpleStringProperty(specialist);
this.workTime = new SimpleStringProperty(workTime);
this.qualification = new SimpleStringProperty(qualification);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。