1

I have a live javafx user application which has a label : No of answers/No of questions , No of answers increases as the user answers the question, and number of questions increases as one class throws questions to user.

Label initially looks like - 0/0

I want to bind two different variables (NumberOfAnswers, NumberOfQuestions) to this label, say if I have 10 questions thrown to user and user has answered 2 it should look like : 2/10

Label ansQuestLbl = new Label("0/0");
    if (answerConnector!= null) {
        log.info("Going to bind , No of answers: "+answerConnector.getNoOfAnswers());
        ansQuesLbl.textProperty().bind(answerConnector.getNoOfAnswers().asString());
        log.info("Bound number of answers with label on UI");
    }

This only binds Number of answers to the label.

Thanks in advance.

Chetna rustagi
  • 463
  • 7
  • 21

2 Answers2

4

All you need is Bindings.concat(Object... args):

For example:

IntegerProperty noOfAnswers = answerConnector.noOfAnswersProperty();
IntegerProperty noOfQuestions = answerConnector.noOfQuestionsProperty();
ansQuesLbl.textProperty().bind(Bindings.concat(noOfAnswers, "/", noOfQuestions));

Or:

IntegerProperty noOfAnswers = answerConnector.noOfAnswersProperty();
IntegerProperty noOfQuestions = answerConnector.noOfQuestionsProperty();
ansQuesLbl.textProperty().bind(noOfAnswers.asString().concat("/").concat(noOfQuestions.asString()));

Note: To avoid problems with the Properties I would recommand to follow the javafx naming convention, so that the class for answerConnector should look like this:

public class AnswerConnector {
    private final IntegerProperty noOfAnswers = new SimpleIntegerProperty(0);

    public IntegerProperty noOfAnswersProperty() {
        return noOfAnswers;
    }

    public int getNoOfAnswers() {
        return noOfAnswers.get();
    }

    public void setNoOfAnswers(int noOfAnswers) {
        this.noOfAnswers.set(noOfAnswers);
    }

    // same for noOfQuestions
}
  • 2
    Alternatively, for more elaborate formatting you can use [`Bindings.format`](https://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/Bindings.html#format-java.lang.String-java.lang.Object...-). – Itai Feb 19 '18 at 08:46
  • I am trying to it on my javafx scene, Do I need to refresh my scene because it is getting bound with label but not updated on my UI ? – Chetna rustagi Feb 19 '18 at 13:05
  • What do you mean with _not updated on my UI_? Where can the values of `noOfAnswers` and `noOfQuestion` be changed? By the way better rename `getNoOfAnsers` to `noOfAnsersProperty`. –  Feb 19 '18 at 13:13
  • Thanks for the suggestion, It is not getting updated on `javafx scene` , as the value of answers changing ,label value is not getting updated on scene/UI. – Chetna rustagi Feb 19 '18 at 13:17
  • Is it possible that you accidentally change the object of `answerConnector` or `getNoOfAnswers` and not the value of the properties? –  Feb 19 '18 at 13:19
  • No i am updating the value only, while debugging value is getting updated also, but not on UI, may be i need to refresh my page or do the bind in separate thread? – Chetna rustagi Feb 19 '18 at 13:22
  • setter is setting it correct as the value is getting updated, only not getting updated on label – Chetna rustagi Feb 19 '18 at 13:38
  • Does your setter for `setNoOfAnsers` look like the one in my answer? –  Feb 19 '18 at 13:51
  • Able to do it now, was doing one mistake, thanks a lot :) – Chetna rustagi Feb 20 '18 at 05:32
2

Bindings.format can be used for this purpose:

ansQuesLbl.textProperty().bind(Bindings.format("%d/%d",
                                               answerConnector.noOfAnswersProperty(),
                                               answerConnector.noOfQuestionsProperty()));
fabian
  • 80,457
  • 12
  • 86
  • 114