Tips for SWT usage



  • After getting frustrated at work with custom Swing tab-ordering ( a real WTF on its own ) I decided to learn a bit about how SWT does its tab ordering.

    After a bit of googling I came up on this little gem for initializing SWT Controls:


    I Especially like the "Iterate over every Control, catch ClassCastException on each one, and proceed on" .. 



  • Lovely. Because "if ( item instanceof Text) .." is so complicated.. Gah. What a craptastic hack..  



  • What, out of curiosity, is this "custom Swing tab-ordering" problem? The tabs appear in the order you add them. Unless you specify an explicit index, in which case the tab appears at that index. (Of course, having more than a few tabs in your window is not a very good UI design, but I'm sure you already knew that.)



  • He's talking about the order controls get focus when you use the tab key on your keyboard, not just a tab control to show different pages in a dialog.



  • I'm curious as to why you think swing tab traversal is a WTF. It seems pretty straightforward to me:

    http://java.sun.com/j2se/1.5.0/docs/api/java/awt/FocusTraversalPolicy.html

    I've never implemented a custom traversal policy, but I know swing very well. Was the FocusTraversalPolicy hard to implement? 



  • I ran into trouble implementing a custom FocusTraversalPolicy for a single JPanel that is nested in another JPanel. What I specifically needed was a different tabbing order for just one panel's components. Just to clarify, the top 'root' panel has three other panels and I wanted to have custom tab ordering in just one of them. The view I was working with is really really complex, with three regular tabbedpanes each containing 6 - 10 panels, each containing the Components. I'm a contractor in the banking industry at the moment :)

    Implementing the tab ordering for one JPanel is easy, but getting the tab ordering to work _between_ panels was difficult, the focus doesn't seem to want to transfer to the first (or last) element of the following/previous panel.

     I got a workaround by manually adding the components to the custom FocusTraversalPolicy, but that involved some crude hacks (such as including components from other panels). I realize the problem lies somewhere with the cycle root switching focus down and up when it reaches the final element of a focus cycle root. I just thought to hell with it and hacked around it :) In the end it only involved changes in three classes to get it working.
     


Log in to reply