Min Htet Oo
Min Htet Oo

Reputation: 536

My simple table view doesn't work in javaFX

I am a beginner to javaFx and I am now inserting data from my Person Objects to my tableView with the help of online toturials.But my code has some problems.There is neither compile time nor run time errors.But there is no data in my tableView.Being a beginner at javaFx,I have checked my code for a long time.But I don't know why it is.Could you take a look at that please?Here is my modal class,Person.java

public class Person {
        public  SimpleStringProperty firstName;
        public SimpleStringProperty lastName;

        public Person(String fName, String lName) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
        }
}

This is my controller class

public class FXMLDocumentController implements Initializable {

    private ObservableList<Person> personData = FXCollections.observableArrayList();

   @FXML
   private TableView<Person> table;
     @FXML
    private TableColumn<Person, String> col1;
    @FXML
    private TableColumn<Person, String> col2;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
         col1.setCellValueFactory(
            new PropertyValueFactory<Person,String>("firstName")
        );

        col2.setCellValueFactory(
            new PropertyValueFactory<Person,String>("lastName")
            );  
        personData.add(new Person("John","Leon"));
        System.out.println("data to collection added");
        table.setItems(personData);
        System.out.println("collection  to table added");


    }    

}

And this is my FXML code

<AnchorPane id="AnchorPane" prefHeight="332.0" prefWidth="409.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="fucktable.FXMLDocumentController">
    <children>
      <TableView fx:id="table" layoutX="42.0" layoutY="54.0" prefHeight="200.0" prefWidth="200.0">
        <columns>
          <TableColumn fx:id="col1" prefWidth="75.0" text="C1" />
          <TableColumn fx:id="col2" prefWidth="75.0" text="C2" />
        </columns>
      </TableView>
    </children>
</AnchorPane>

Thanks for your attention

Upvotes: 0

Views: 595

Answers (1)

Madushan Perera
Madushan Perera

Reputation: 2598

You should have these methods inside your Person class:

public String getFirstName() {
    return firstName.get();
}

public void setFirstName(String s) {
    firstName.set(s);
}

public String getLastName() {
    return lastName.get();
}

public void setLastName(String s) {
    lastName.set(s);
}

Upvotes: 1

Related Questions