make JavaFX Window Always On Top - Java JavaFX

Java examples for JavaFX:Stage

Description

make JavaFX Window Always On Top

Demo Code


//package com.java2s;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import javafx.stage.Stage;
import javafx.stage.Window;

public class Main {
    public static void makeAlwaysOnTop(final Window window) {
        window.focusedProperty().addListener(new ChangeListener<Boolean>() {

            @Override/*from w w  w  .j a v  a 2 s.  c  o m*/
            public void changed(
                    ObservableValue<? extends Boolean> observable,
                    Boolean oldValue, Boolean newValue) {
                if (!newValue.booleanValue()) {
                    window.requestFocus();
                }
            }

        });
    }

    public static void makeAlwaysOnTop(final Stage stage) {
        stage.focusedProperty().addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(
                    ObservableValue<? extends Boolean> observable,
                    Boolean oldValue, Boolean newValue) {
                if (!newValue.booleanValue()) {
                    stage.requestFocus();
                    stage.toFront();
                }
            }

        });
    }
}

Related Tutorials