0

I tried to write my 'own' log in a tab and I experienced problems with updating the label with a naive solution that I had.

So after I googled I checked out this solution here: Displaying changing values in JavaFx Label

I don't know if I did everything right but unfortunately this solution doesn't work for me.

final static Label logLabel = new Label();
final static SimpleStringProperty logString = new SimpleStringProperty("test");
...
...
     public void start(Stage primaryStage) {


    TabPane tabPane = new TabPane();
    tabPane.getTabs().add(createSettingsTab());
    tabPane.getTabs().add(createParticipantTab());
    tabPane.getTabs().add(createSpectatorTab());
    tabPane.getTabs().add(createOverviewTab());
    tabPane.getTabs().add(createTournamentTab());
    tabPane.getTabs().add(createLogTab());
    tabPane.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);


    Scene scene = new Scene(tabPane, 1200, 800);

    primaryStage.setScene(scene);
    primaryStage.setResizable(true);




    primaryStage.show();
}


private Tab createLogTab() {
// TODO Auto-generated method stub
            logLabel.textProperty().bind(logString);    
            Tab tab = new Tab("Log");
            tab.setContent(logLabel);
            return tab;
        }

I got this lines for initializing the Label and for setting the new text I do this:

logString.set(logString.get() + "foo");

The log tab keeps being blank...

I'd appreciate any help! Thanks!

edit// That's the only useful mcve I can think of. As I said the other create methods to create the other tabs are not making use of the label or the SimpleStringProperty

This is the button that doesn't work as supposed concerning the label.

    buttonLoadConfig.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent e) {
                    System.out.println("test");
                    logString.set(logString.get() + "\ttest");

                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("Choose Config");
                    File config = fileChooser.showOpenDialog(new Stage());
    }
});
Community
  • 1
  • 1
r.valbo
  • 25
  • 6
  • It's unlikely anyone will be able to help with this until you edit your question to include an [MCVE](http://stackoverflow.com/help/mcve) – James_D Jun 27 '15 at 14:50
  • Sorry, I thought this example would be sufficient, because the whole code is too long to post it here and I didn't where exactly the problem was. But I figured out the weak point. The button I tested this with opens a FileChooser and thats probably causing the problem as with another button I created to test it the problem didn't occur. – r.valbo Jun 27 '15 at 18:19
  • No one asked for your whole project; you should always create an MCVE for questions of this type. – James_D Jun 27 '15 at 20:06

1 Answers1

1

Use of static members is often not a good idea and is probably contributing to your issue.

Your code as it is currently written can have many tabs but only one label. You are trying to add the same label to multiple tabs. But, when you do that, the label will be removed from the previous tabs because of rules on how the JavaFX Scenegraph works.

See the Node documentation:

If a program adds a child node to a Parent (including Group, Region, etc) and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent.

So the binding is working, but, due to the above rule and the way you have written the rest of your code, the program as a whole is likely not working as you expected or wished.

If you are still having issues, update your question with an mcve.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • The log tab that I'm creating by calling this method is the only tab that I'm connecting the label to and the createLogTab method is also called just once. So I think this shouldn't be the problem if I'm not mistaken. – r.valbo Jun 26 '15 at 20:21
  • Then edit your question to include an mcve as suggested. – jewelsea Jun 26 '15 at 20:42