I wanted to create a method that would return me a table in BorderPane ,but I got the empty table and the label of it shown on the GUI. Here's the code:
@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
}
This is how I create the table data and the method by following the documentation:
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;
}
And this is the shortened version of the class I created. There are setters and getters.
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);
}
}