0

I have an ObservableList of instances of class GameLogEntry:

public class GameLogEntry {
    private int gameNumber;

    private int chosenStratA;
    private int chosenStratB;

    private Double[] scoresByB;
    private Double[] scoresByA;

    private double higherPrice;
    private double lowerPrice;
    private double averagePrice;

    private int maxIndex;

In my program I populate TableView's columns with values from this ObservableList. enter image description here

My columns:

@FXML
    private TableColumn<GameLogEntry, String> colGameNum;

    @FXML
    private TableColumn<GameLogEntry, String> colChosenStratA;

    @FXML
    private TableColumn<GameLogEntry, String> colScoreByBOne;

    @FXML
    private TableColumn<GameLogEntry, String> colScoreByBTwo;

    @FXML
    private TableColumn<GameLogEntry, String> colChosenStratB;

    @FXML
    private TableColumn<GameLogEntry, String> colScoreByAOne;

    @FXML
    private TableColumn<GameLogEntry, String> colScoreByATwo;

    @FXML
    private TableColumn<GameLogEntry, String> colLowerPrice;

    @FXML
    private TableColumn<GameLogEntry, String> colHigherPrice;

    @FXML
    private TableColumn<GameLogEntry, String> colAveragePrice;

I'm doing it like this in initialize method:

colGameNum.setCellValueFactory(new PropertyValueFactory<GameLogEntry, String>("gameNumber"));
colChosenStratA.setCellValueFactory(new PropertyValueFactory<GameLogEntry, String>("chosenStratA"));
colChosenStratB.setCellValueFactory(new PropertyValueFactory<GameLogEntry, String>("chosenStratB"));
colHigherPrice.setCellValueFactory(new PropertyValueFactory<GameLogEntry, String>("higherPrice"));
colLowerPrice.setCellValueFactory(new PropertyValueFactory<GameLogEntry, String>("lowerPrice"));
colAveragePrice.setCellValueFactory(new PropertyValueFactory<GameLogEntry, String>("averagePrice"));

But also I need to fill CS(B1), CS(B2), CS(A1) and CS(A2) columns.

CS(B1) should be filled with scoresByB[0],

CS(B2) - with scoresByB[1],

CS(A1) - with scoresByA[0],

CS(A2) - with scoresByA[1].

The question is - can I do it with the same simple methods like I did with other values? And if no, how can I do it in any other ways?

4
  • Possible duplicate of Inserting data to JavaFX TableView without intermediate class Commented Sep 9, 2018 at 14:49
  • @Sedrick I don't really understand how that question is related to mine. If you think it's about the same problem, could you please explain it to me in the context of my question? Commented Sep 9, 2018 at 15:00
  • You are right. Sorry wrong answer. Commented Sep 9, 2018 at 15:03
  • stackoverflow.com/questions/20769723/… this is closer to waht y ou are asking Commented Sep 9, 2018 at 15:06

1 Answer 1

2

Simply implement the factory yourself. You could create a helper method to avoid repeating the code:

static <S, T> Callback<TableColumn.CellDataFeatures<S, T>, ObservableValue<T>> createArrayValueFactory(Function<S, T[]> arrayExtractor, final int index) {
    if (index < 0) {
        return cd -> null;
    }
    return cd -> {
        T[] array = arrayExtractor.apply(cd.getValue());
        return array == null || array.length <= index ? null : new SimpleObjectProperty<>(array[index]);
    };
}
colScoreByBOne.setCellValueFactory(createArrayValueFactory(GameLogEntry::getScoresByB, 0));
colScoreByBTwo.setCellValueFactory(createArrayValueFactory(GameLogEntry::getScoresByB, 1));
colScoreByAOne.setCellValueFactory(createArrayValueFactory(GameLogEntry::getScoresByA, 0));
colScoreByATwo.setCellValueFactory(createArrayValueFactory(GameLogEntry::getScoresByA, 1));

For this to work however you need to change the types of the column to TableColumn<GameLogEntry, Double>. (In fact you should change all the value types for the columns, since not a single property is of type String; all properties are of type Double or Integer.)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for pointing out that String type was wrong. I had some doubts about it.
certainly possible but: if the individual elements of an array have some meaning, they should be modelled in an adapter to the log, not ad-hoc in a utility method
@kleopatra can you explain me what did you mean by that adapter thing? I'm quite new in Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.