简体   繁体   中英

JavaFX: How do you display the contents of a String[] in a TableColumn?

I have a class for movies. The actors are saved in a String[]. I created a GUI with the SceneBuilder. I am setting the values of each TC like so:

    tableDirector.setCellValueFactory(new PropertyValueFactory<>("Director"));
    tableActors.setCellValueFactory(new PropertyValueFactory<>(("actors")));

That works for all the non-Collections stuff, but I cannot for the love of everything that's holy figure out how to put several Strings from a String[] in a single Column. Am I missing something?

Assuming all the code you haven't shown is correct, you can do

tableActors.setCellFactory(tc -> new TableCell<Movie, String[]>() {
    @Override
    protected void updateItem(String[] actors, boolean empty) { 
        super.updateItem(actors, empty);
        if (empty || actors == null) {
            setText(null);
        } else {
            setText(String.join(", ", actors));
        }
    }
});

A more sophisticated cell factory might produce a cell that displayed the actors in a ListView , or some similar option.

Note that the cellFactory is in addition to the cellValueFactory you have already defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM