Tarasov
Tarasov

Reputation: 3695

How I can bind my List to a TableView in JavaFX?

I want to use TableView with a List in javaFX. It work to 90%.

My problem is that if I load my data to TableView the second row I the same how the first...I think I have a problem in my foreach.

Here My Code:

@FXML
    private TableView<Ticket> tblTicket = new TableView<>();

...


@Override
    public void initialize(URL url, ResourceBundle rb) {

        try {
               ...

                tblTicket.getColumns().addAll(Ticket.getColumn(tblTicket));

                tblTicket.setItems(getTicketData());

        } catch (Exception e) {
           ...
        }
    }    


...

public ObservableList<Ticket> getTicketData() {

        //Eine Liste mit allen Tickets
        ObservableList<Ticket> ticketData = FXCollections.observableArrayList();

        ticketData.setAll(new Ticket("1", "Fehler 1", "Meier_A", "01.02.2015", "Offen"));
        ticketData.setAll(new Ticket("2", "Fehler 2", "Schmidt_W", "01.02.2015", "In Bearbeitung"));
        ticketData.setAll(new Ticket("3", "Fehler 3", "Tarasov_W", "01.02.2015", "Geschlossen"));

        return ticketData; 
    }

...

I make it how in this video but my table is empty.

Upvotes: 0

Views: 5772

Answers (1)

varren
varren

Reputation: 14741

I think you just need to set something like this in your initialize() instead of for loop and there is no need in ticketData.size()>0

public void initialize(){ //added after James_D comment about initialize()
    clmID.setCellValueFactory(new PropertyValueFactory<>("ticketId"));
    clmTicketName.setCellValueFactory(new PropertyValueFactory<>("ticketName"));
    clmLastName.setCellValueFactory(new PropertyValueFactory<>("ticketLastName"));
    clmCategory.setCellValueFactory(new PropertyValueFactory<>("ticketCategory"));
    clmFirstName.setCellValueFactory(new PropertyValueFactory<>("ticketFirstName"));
}

Edit:

Here is demo: https://github.com/varren/How-I-can-bind-my-List-to-a-TableView-in-JavaFX/tree/master/src/sample

Here is tutorial: https://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm#CACFEHBI

Upvotes: 2

Related Questions