Tab ordering

Dear Juce-ers, :slight_smile:

What is the recommended/easiest way to modify the “tab-order” of a Jucer-designed component? In other words, the order in which controls are edited when using the tab key to change focus.



There’s nothing in the jucer to do that at the moment, it uses the normal KeyboardFocusTraverser algorithm to move the focus about. (Left-to-right and then top-to-bottom, I think it is).

If you need really customised movement, you can override Component::createFocusTraverser and create any kind of algorithm.

One thing I’ve been meaning to do is to add an extra focus order value to components to make it easy to edit simple focus orders in the jucer.

Hi Jules,

That’d be very useful. :slight_smile:

In the meantime, do I simply need to change the order in which the items are listed in the XML block embedded within the file; and is this “safe” for me to re-order (from a perspective of not confusing Jucer when it comes to re-edit)?


Well no, I think the default mechanism uses their positions to choose the order, not their z-order (which is all that would change if you move them round in the file).

Aha! In which case, an optional focus order property would be VERY useful. :slight_smile:

That would allow a GUI layout person to tinker with component layout/ordering using Jucer, without the code needing to be touched by a coder…

Oh well. Always too many things that could be done, eh? :slight_smile:

you can say that again…

Hi Jules,

Many thanks for adding the tab-ordering support to the Jucer/framework!!

On thing I’ve noticed though, in juce_KeyboardFocusTraverser.cpp:

class ScreenPositionComparator
    ScreenPositionComparator() {}

    static int compareElements (const Component* const first, const Component* const second) throw()
        int explicitOrder1 = first->getExplicitFocusOrder();
        if (explicitOrder1 < 0)
            explicitOrder1 = INT_MAX / 2;

        int explicitOrder2 = second->getExplicitFocusOrder();
        if (explicitOrder2 < 0)
            explicitOrder2 = INT_MAX / 2;

        if (explicitOrder1 != explicitOrder2)
            // Ooops - this is the wrong way round!
            // return explicitOrder2 - explicitOrder1;
            // Use this instead...
            return explicitOrder1 - explicitOrder2;

        const int diff = first->getY() - second->getY();

        return (diff == 0) ? first->getX() - second->getX()
                           : diff;

You’ll see that I’ve fixed the code where it says “Ooops”… this is assuming we want pressing tab to move through focus elements 1, 2, 3… in order (rather that in reverse order, like we have at the moment!).

HTH! :slight_smile:


Could have sworn that I tested that! But looking at it now, there’s actually another bug in there that probably explains why I thought it was working… I’ve checked in a fix now.

Yep, that worked for me. Many thanks!! Pete