Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

java - JavaFX 2.2 get node at coordinates (visual tree hit testing)

How can I get a list of (or the first one of) the controls under the mouse/arbitrary coordinates? I know WPF has VisualTreeHelper.HitTest and it has a callback that can be used to filter out all the controls at a point. Is there something similar for JavaFX? (or different, I just care for the first element at a given point) I've seen lots of information to get coordinates of a node, but no information on how to get a node by coordinates.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use node.impl_pickNode(x,y).

Documentation from the impl_pickNode method (copied from the source).

/** 
 * Finds a top-most child node that contains the given coordinates.
 *
 * Returns the picked node, null if no such node was found.
 *
 * @deprecated This is an internal API that is not intended for use 
 *             and will be removed in the next version. 
 **/
public final Node impl_pickNode(double parentX, double parentY)

Note carefully the deprecation warning in the comment and use at your own risk.

Update

There is an existing feature request in the JavaFX issue tracker: FX should provide a Parent.pick() routine. This feature request is for a public picking API which will not be deprecated in the future. The requested feature is described as: "The routine could return a single Node or a list of all the Nodes below the mouse ordered by z coordinates". The feature is not scheduled for implementation until the "Van Ness" release which is the release after the initial JDK8 release (i.e. the feature won't be available until Christmas 2013 at the earliest).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...