Receive notification when focused scene changes

I am creating a menu window for JavaFX, similar to the default Window menu in OS X. This menu contains a list of windows for the application and uses a checkmark to indicate the current focus.

I get a list of all windows from

StageHelper.getStages()

which gives me ObservableListto automatically respond to deleted or newly created windows. It remains to note the menu item with the window currently focused. I do not want to actively check the focused scene, since I need to be notified as soon as the focused sphere changes.

My current idea would be to add a focus listener to every stage that was created, however I was wondering if there was anything else. Maybe some auxiliary class that has an observable property for the stage that is currently focused?

+4
source share
1 answer

Firstly, I highly recommend not using a non-public API. You should probably create a class StageFactoryby initializing it in your main stage and requesting new stages from it. Then it can track all the steps for you (and can also easily track a focused scene).

, ObservableList<Stage> focusedProperty . , .

SSCCE:

import com.sun.javafx.stage.StageHelper;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TrackFocusedStage extends Application {

    int stageCount = 1 ;
    @Override
    public void start(Stage primaryStage) {
        ObservableList<Stage> stages = FXCollections.observableArrayList(stage -> new Observable[] {stage.focusedProperty()});
        Bindings.bindContent(stages, StageHelper.getStages());
        Label label = new Label();
        stages.addListener((Change <? extends Stage> c) -> 
            label.setText(
                    stages.stream()
                    .filter(Stage::isFocused)
                    .findAny()
                    .map(Stage::getTitle)
                    .orElse("")));

        Button newStageButton = new Button("New Window");
        newStageButton.setOnAction(e -> newStage());
        Scene scene = new Scene(new BorderPane(new StackPane(newStageButton), label, null, null, null), 300, 180);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Primary");
        primaryStage.show();
    }

    private void newStage() {
        Stage stage = new Stage();
        stage.setTitle("Window "+(++stageCount));
        Button close = new Button("Close");
        close.setOnAction(e -> stage.hide());
        stage.setScene(new Scene(new StackPane(close), 300, 80));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
+4

All Articles