JavaFX Make object visible but not consume (ignore) clicks

心已入冬 提交于 2019-12-08 19:10:52

问题


Title says it all: I have a rectangle in JavaFX (for transparency/opacity effects) yet I want people to be able to 'click through' it.

So when I say click through, I mean when you click it, Java should pretend it isn't there and instead 'click' on whatever is beneath that object. When you make something invisible with setVisible(false), this is exactly what happens - whatever object you set invisible doesn't consume the click events that your mouse generates. However, the object becomes invisible - something I do not want.

Is it possible? Thankyou for any help you can provide!


回答1:


All Nodes in JavaFX have a setMouseTransparent() method, as detailed here, where the mouseTransparent property is:

If true, this node (together with all its children) is completely transparent to mouse events. When choosing target for mouse event, nodes with mouseTransparent set to true and their subtrees won't be taken into account.

If you need more complex mouse transparency effects, such as having only the parent node be transparent, you may need to combine that with some other method dealing with click detection (its name escapes me at the moment I think it was pickOnBounds; as for how to use it, I can't say for sure. I think I more abused it by setting literally everything in my app to either true or false, but I don't remember which one, and unfortunately wouldn't be able to tell you what nodes you'd need to set that property for), but for simple mouse transparency that should work.



来源:https://stackoverflow.com/questions/24164667/javafx-make-object-visible-but-not-consume-ignore-clicks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!