How to get JavaFX 2.0 visibility event on Pane?

Jakez picture Jakez · Jul 26, 2013 · Viewed 8k times · Source

After several tries and researches, I have not managed to get visibility event from Pane. The below sample seems to be my best try, but it does not work. Any working propositions are welcomed.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class VisibilityTestMain extends Application {

public static void main(String[] args) {
    VisibilityTestMain.launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    Pane root = new Pane();
    ChangeListener<Boolean> visibilityListener = new ChangeListener<Boolean>() {

        @Override
        public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldValue, Boolean newValue) {
            System.out.println("####");
        }
    };
    root.visibleProperty().addListener(visibilityListener);

    Button button = new Button("Hello");
    button.setTranslateX(10);
    button.setTranslateY(20);
    root.getChildren().add(button);

    stage.setScene(new Scene(root, 50, 50));
    stage.show();

}

}

Answer

Fabinout picture Fabinout · Jul 26, 2013

The problem is that you never change the visibility of your Pane, thus your listener is never reached.

Try this piece of code instead:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VisibilityTestMain extends Application {

public static final Logger LOGGER = LoggerFactory.getLogger(VisibilityTestMain.class);
public static void main(String[] args) {
    VisibilityTestMain.launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    final Pane root = new Pane();
    root.visibleProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(final ObservableValue<? extends Boolean> observableValue, final Boolean aBoolean, final Boolean aBoolean2) {
            System.out.println("####");
            //To change body of implemented methods use File | Settings | File Templates.
        }
    });

    Button button = new Button("Hello");
    button.setTranslateX(10);
    button.setTranslateY(20);
    root.getChildren().add(button);
    button.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(final ActionEvent actionEvent) {
            root.setVisible(false);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            root.setVisible(true);
        }
    });

    stage.setScene(new Scene(root, 50, 50));
    stage.show();

}
}

Now you can see that the visible property of your Pane is accessed every time you click your button.