-2

I have a program with these columns:

showNameColumn.setCellValueFactory(new PropertyValueFactory<Show, String>("ShowName"));
epsWatchedColumn.setCellValueFactory(new PropertyValueFactory<Show, Integer>("EpsWatched;"));
showStatusColumn.setCellValueFactory(new PropertyValueFactory<Show, String>("ShowStatus"));

The getters are called getShowName(), getEpsWatched(), and getShowStatus() which get the variables showName, epsWatched, and showStatus.

What's wrong here? The strings display but not the ints.

  • @jewelsea instead of int didn't do anything (not sure why it would, it's a wrapper class). Also how would I write a lamba for this? I've never used them before (I know of them, just haven't used them), and the example thread you listed is a bit hard for me to follow. I got: showNameColumn.setCellValueFactory(cellData -> cellData.getValue().getShowName()); When I tried to mess with it, but this (probably obviously) gives a type mismatch error of cannot convert from String to ObervableValue. – JustSomeoneWhoCodes Nov 19 '21 at 09:58
  • that was the issue lol, can you write that as an answer so I can credit you for answering this question? – JustSomeoneWhoCodes Nov 19 '21 at 09:59
  • Integer vs int was just a guess. You can rely on the autoboxing behavior most of the time, and in this case I guess it didn’t matter. But int and Integer are not the same thing and I don’t like to trust autoboxing if I don’t have to (just personal preference). – jewelsea Nov 19 '21 at 10:09

1 Answers1

1

You have a “;” in the string passed to the property value factory, it should not be there.

This is wrong:

new PropertyValueFactory<Show, Integer>("EpsWatched;")

PropertyValueFactory lookup strings and property names should follow Java bean naming conventions. For more info, see:

Normally, I’d close the question as a duplicate of the above question, however, the asker requested a specific answer and had some additional questions, so this answer addresses those directly.

It is recommended to use a lambda rather than a PropertyValueFactory, as any errors will be detected and reported accurately by the compiler rather than failures at runtime. For a thorough analysis, see:

how would I write a lamba for this?

In your model class declare the property and provide an accessor for it:

public class Show {
    private final IntegerProperty numEpisodes = new SimpleIntegerProperty();
    public final IntegerProperty numEpisodesProperty() {
        return numEpisodes;
    }
}

Use the property accessor in a lambda to define the cell value factory:

TableColumn<Show, Integer> numEpisodesCol = new TableColumn<>();
numEpisodesCol.setCellValueFactory(
    data -> data.getValue().numEpisodesProperty().asObject()
);

See also:

jewelsea
  • 150,031
  • 14
  • 366
  • 406