1

I am trying to add event handler to the stage's title bar, but it won't work.

I tried using this:

primaryStage.addEVentHandler(MouseEvent.MOUSE_PRESSED, (event)->{
     System.out.println("ok");
});

But it only register the event to the scene's area and won't read the mouse pressed event on the title bar.

Is there a way to listen the mouse event on stage's title bar?

Complete code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class demo extends Application {

    @Override
    public void start(final Stage primaryStage) {
        HBox box = new HBox();
        Scene scene = new Scene(box, 350, 250);

        Text text = new Text("Test");
        box.getChildren().add(text);

        primaryStage.addEventHandler(MouseEvent.MOUSE_PRESSED,(event) -> {
            System.out.println("ok");
        });
        primaryStage.setScene(scene);
        primaryStage.setTitle("TimePicker");
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }    
}
Ari
  • 4,643
  • 5
  • 36
  • 52

2 Answers2

0

As far as I know, this is not possible, as the user expects a certain behaviour from interaction with the titlebar as determined by the underlying OS. If you start handling mouse events on the titlebar, you would likley interfere with this expected behaviour.

This said, by hiding the titlebar by setting primaryStage.initStyle(StageStyle.UNDECORATED); it would be possible to implement your own title bar, that provides your custom behaviour.

Mirko Fetter
  • 186
  • 1
  • 13
0

I couldn't figure out how to do anything with the system default title bar. It's not a node and it doesn't respond in a negative coordinate space.

Possible workaround: Create your own title bar!

Make your primary stage and style it as StageStyle.UNDECORATED.

Use a root pane like BorderPane to develop your own title bar at the top.

See this link for making an UNDECORATED stage draggable (basically dragging the top of your BorderPane).

Good luck!

Eric
  • 601
  • 7
  • 22