• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use ContainerOrderFocusTraversalPolicy?

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've found that, by default, the focus traversal policy (hereafter "tab order") in forms and panels doesn't follow the order in which controls were inserted into the container, but is derived from the position of the components on the form or panel. This is very neat (and probably allows sloppy coders and GUI builders to exist without actually ever thinking about the tab order), except when it isn't. As when you actually want to specify a different component order, for example.

In the following example, I've created a form with two columns of buttons. I want the tab order to go through the first column of buttons, followed by second column of buttons (ie. a column-by-column schema). The default tab order is row-by-row, however, and can be obtained for reference by commenting out the setupFocus() call in the constructor.

I had hoped that the ContainerOrderFocusTraversalPolicy would do the job, but there is a couple of problems (which I've addressed in the setupFocus() method). Firstly, the container itself is part of the focus chain. This at least is easily remediable by calling setFocusable(false), but I don't have to do that with the initial focus traversal policy, so I wonder why I have to do it with this one. The other problem is more pressing, though - the ContainerOrderFocusTraversalPolicy lets me (un)hapilly tab through JLabels. Again, I've fixed this, but the initial policy knows all by itself that it's not a good idea to focus a JLabel. Moreover, I'm afraid there might be other components that do not receive focus with the original policy, but ContainerOrderFocusTraversalPolicy might plod though them.

So, basically my question is: isn't there some focus traversal policy implementation that I could just set and it would tab through exactly the same components as the original policy, except it would order them according to their order in the container?

Code:
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update: I tried to figure out how the default focus traversal policy determines which components can receive focus. It looks like the LayoutFocusTraversalPolicy is the default on forms and panels, and the following monster method in this class is responsible for determining whether a component can receive focus:

Uh oh. I wonder why this methods is not implemented somewhere up the hierarchy, so that all the various focus traversal policy implementations could just use it.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By now I'm pretty sure there isn't an easier way. I'll probably create a child of the ContainerOrderFocusTraversalPolicy with the accept method copied from LayoutFocusTraversalPolicy and file it as another one of my generally useful Swing classes. If someone knows about a better solution, I'm still interested, though.
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic