4lex1v
4lex1v

Reputation: 21557

Change stage titleProperty on selecting different tabs in javafx

I guess there should be some event for this, but didn't find any. The code I have now is

stage.titleProperty().bind(tabPane.getSelectionModel().getSelectedItem().textProperty());

But it doesn't change the title dynamically, what's the right solution?

Upvotes: 2

Views: 2287

Answers (2)

jewelsea
jewelsea

Reputation: 159341

Puce's explanation is correct. Here is a change listener example.

stage.setTitle(tabPane.getSelectionModel().getSelectedItem().getText());
tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
  @Override public void changed(ObservableValue<? extends Tab> tab, Tab oldTab, Tab newTab) {
    stage.setTitle(newTab.getText());
  }
});

Use the above code instead of the sample code in your question.

Upvotes: 3

Puce
Puce

Reputation: 38132

I can't give you a solution right now, but I think I spot the problem: You've bound the titleProperty to the textProperty of the tab which was selected at binding time. You probably need to listen for selection changes and change the stage title in the listener.

Upvotes: 1

Related Questions