-3

Info : Java 8x64 running on macOS Mojave

Problem : Cannot click through transparent background on javafx stage.

Code :

VBox root = new VBox();
AnchorPane background = new AnchorPane();
background.setPrefSize(400, 400);
background.setMinSize(400, 400);
background.setMaxSize(400, 400);
background.setClip(new 
javafx.scene.shape.Rectangle(400,400));
root.getChildren().add(background);

Scene scene = new Scene(root);
Stage stage = new Stage();
scene.setFill(javafx.scene.paint.Color.TRANSPARENT);
stage.setScene(scene);
stage.setAlwaysOnTop(true); //remove on java7
stage.initStyle(StageStyle.TRANSPARENT);
stage.show();

This is just a test code to show you, you will not be able to click through this view which is not visible. The same code in java 7x86 on Windows will work (you will be able to click through).

hugo411
  • 320
  • 1
  • 12
  • 29
  • 2
    Could you please explain your intention a little bit more, and plesae post [MCVE](https://stackoverflow.com/help/mcve) so you can get more help. Are you just trying to show a dialog box? You can use [JOptionPane](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html). Please give an update what you trying to do in more comprehensive way Please also give an update about `root` variable – Akiner Alkan Jan 14 '19 at 14:49
  • please look at this if you don't know. Dont lower vote if you have no idea. https://bugs.openjdk.java.net/browse/JDK-8088104 – hugo411 Jan 14 '19 at 21:30
  • I did not downvote the question, but without further info it is really hard to answer your question properly, just wanted to let you know that is why I commented to help you out. – Akiner Alkan Jan 15 '19 at 06:00
  • I am trying to do something very easy on Windows, but it seems there is a bug on macOS. Transparency doesn't work the same way. I found a lot of unanswered questions about this. I just want to show a button on my screen, when i hover on it, a view appear just over it. I usually do a transparent background with a setVisible(false) AnchorPane. Even when no visibility, i can still click on it. Same code on Windows doesn't do that. – hugo411 Jan 15 '19 at 14:01
  • I modified my question, sorry for the delay – hugo411 Jan 15 '19 at 17:19
  • 2
    Two things. (1) A proper [mcve] that people can copy-paste and run _as is_ will improve your question. (2) Are you sure macOS supports the functionality you want? – Slaw Jan 17 '19 at 10:54
  • Well this is what i am asking, if this is not possible then is there an alternative. The code can be copy-paste inside a fresh javafx app (MCVE), please try before. You will have a transparent rectangle which cannot be click through on macos (java 8). – hugo411 Jan 17 '19 at 13:46
  • I just made the test again, i copy-paste my code in windows java 8, i can select text under the transparent rectangle.Same code on macOS java 8, i can't select text under the transparent rectangle. – hugo411 Jan 17 '19 at 14:01
  • Before your most recent edit your code didn't even exemplify the problem (your title mentioned `JFXPanel`, indicating Swing interop, but your code didn't show that). And technically it isn't complete because it won't compile as is; I'd have to add imports as well as the `Application` infrastructure. It may seem trivial and pedantic but when asking for help you should make it as easy as possible for people to help you. – Slaw Jan 17 '19 at 15:01
  • What happens if you set the `Rectangle` to be mouse transparent? [This question](https://stackoverflow.com/questions/17950657/) mentions something similar though it's not for JavaFX. Unfortunately, I don't have a Mac available to test myself. – Slaw Jan 17 '19 at 15:03
  • I tried setMouseTransparent(true), not working :(. Looks like i need os specific code but no idea how. – hugo411 Jan 17 '19 at 15:12
  • Just tested on windows, i can click through and macos i can't. – hugo411 Jan 17 '19 at 16:17
  • See [JDK-8088104](https://bugs.openjdk.java.net/browse/JDK-8088104). – Slaw Jan 23 '19 at 17:47
  • Yes, still a bug today on java 11. – hugo411 Jan 25 '19 at 16:00

2 Answers2

0

if you're using javafx, it might be worth using a conventional stage instead of a jfxpane. You can create a transparent stage using:

Stage s = new Stage(StageStyle.TRANSPARENT);
EverNight
  • 964
  • 7
  • 16
  • yes, if i create a transparent stage and add an empty square scene, i will not see it at all but i can still click it. On windows, the same thing is not clickable. I have a speaker button on my screen, when the mouse hover on it, a view appear on top of it. When my view is hidden, i can still click on it. I don't want that. It's hard to explain but this seems to be only on mac. – hugo411 Jan 14 '19 at 17:30
  • It seems that the OS registers the stage being there. I think it's because you set the fill (even if transparent) so it registers the space between the perimeter as well as opposed to only the perimeter lines when it's not filled. You can test this by making the perimeter lines thicker and see if clicking on them behaves the same way. ... right, so why not move the pop-up (thing that shows up when you hover over the sound icon) to an off-screen location (1981:1081) when you don't want it to be clickable? – EverNight Jan 17 '19 at 16:08
  • change the code for setFill(null), it will do the exact same thing. I tried to setLayoutX or Y on the node but it doesn't seem to be moving. – hugo411 Jan 17 '19 at 19:14
0

Edit: Problem you are describing is clearly a low-level bug. I don't think anyone on SO would have a workaround for it.

Maybe what you are looking for is mouseTransparentProperty Setting it to true will cause the node to not receive any mouse events. It works whether your component is visible or not.

Siddhesh Rane
  • 419
  • 3
  • 7
  • Well if this is a bug, this is what i want to know. Any workaround also is appreciated. setMouseTransparent is not working, still blocking the os. – hugo411 Jan 21 '19 at 16:42
  • I put the edit after I realized `setMouseTransparent` is not necessary. I don't think there can be any workaround without modifying the low level code. – Siddhesh Rane Jan 21 '19 at 16:46