JavaFX – Z-order of Nodes

Z-order is an ordering of overlapping two-dimensional objects, such as windows in a graphical user interface (GUI) or shapes in a vector graphics editor. One of the features of a typical GUI is that windows may overlap, so that one window hides part or all of another. When two windows overlap, their Z-order determines which one appears on top of the other. courtesy: wikipedia

JavaFX provides toFront() and toBack() methods in Node to change the Z-order. But users may want more control on how to order the nodes.

For Applet mode, click on above image

For standalone mode

In above sample, the Rectangles are stacked. The node is brought to top when user clicks on it. ActionScript provides DisplayObjectContainer.setChildIndex(child, index). Silverlight provides ZIndexProperty which can be set using SetValue method. Looks like its two different approach, one is done on Container and another on Node. So what is best way to address this in JavaFX? Here is an attempt…

In JavaFX, a sequence of Node is added to Group. So we could change the Z-order by sorting the sequence. This may be done using utility method – sort – available in javafx.util.Sequences.

The sort method is expecting an instance of java.lang.Comparable, for this we can create a CustomNode which implements Comparable and implement compareTo method. Also we can add an additional attribute zOrder. The comparison can be done based on this zOrder attribute. Please refer to NodeZ implementation for more information.

Now we can manipulate the Z-order by updating UI with node array returned by Sequences.sort(). The comparator may also be implemented based on various other attributes such as scale, translate, opacity etc. Yes, there may be some concern regarding updating of content in Group for every sort. What will be its implications? I need to perform some analysis on that, will try to share some info soon..

Try it out and let me know feedback and other options..

var dzone_style = ‘2’;


About Rakesh Menon

6 Responses to JavaFX – Z-order of Nodes

  1. Jens Kloppenburg says:

    Hey, nice solution for this problem! I didn’t think about it that way up to now… (instead I was struggeling with toFront() 🙂 )
    I will definitely try this out!

  2. Rakesh Menon says:

    @Jens Kloppenburg Thanks! Please try it out and let me know if you see any performance issues.

  3. Bob Donyl says:

    Seems there’s a bug. I just clicked on the cyan rectangle at the bottom, and the light brown under the top rectangle also moved

  4. Rakesh Menon says:

    @Bob Donyl Thanks for feedback. I think that is expected, due to simple logic of swap in The clicked index is swapped with top most node. The logic may be updated so as to update all z-order index.

  5. David says:

    I decided to drag the tiles around a little bit.
    It seems like when I click on a tile to make it come to the top, the previous tile is sent to the bottom of the stack, instead of merely making the newly selected tile come to the top of the stack.
    I am uncertain why the previous selected tile needs to have any update done to it.
    I would think that the algorithm should only touch the newly selected tile.

  6. Rakesh Menon says:

    @Bob Donyl, David I think the swap logic is confusing, so I updated the code to just bring the selected node to top. But if this is the only requirement then its better to use toFront()

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: