{"id":610,"date":"2012-11-16T09:01:51","date_gmt":"2012-11-16T07:01:51","guid":{"rendered":"http:\/\/www.pellissier.co.za\/hermien\/?p=610"},"modified":"2012-11-16T09:01:51","modified_gmt":"2012-11-16T07:01:51","slug":"exclude-components-from-tab-order","status":"publish","type":"post","link":"https:\/\/www.pellissier.co.za\/hermien\/?p=610","title":{"rendered":"Exclude Components from Tab Order"},"content":{"rendered":"<p>Today I set out to accomplish something that appeared to be quite simple. However, it took me quite a few hours and lots of reading to find a solution. I do hope that this will be useful to other developers!<\/p>\n<p>I have been working a JPanel form with a lot of different components on it, including JTextFields, JDateChoosers and some custom Swing components. And today I wanted to exclude some of the controls from the tab order. The controls I wanted to exclude were enabled but set as not editable, to allow the user to copy the values as required.<\/p>\n<p>The first thing I tried was setting the components as not focusable. However, that meant that I couldn&#8217;t select the text with the mouse pointer anymore. So back to the drawing board.<\/p>\n<p>I moved on to reading about the FocusTraversalPolicy mechanism (which I have never used before). And after much trial and error, I found this very simple solution. Just extend the class LayoutFocusTraversalPolicy, overriding the accept method as required. Here is the newly created class:<\/p>\n<p>[java]public class ExclusionFocusTraversalPolicy<br \/>\n        extends LayoutFocusTraversalPolicy {<\/p>\n<p>    private ArrayList components = new ArrayList();<\/p>\n<p>    public void addExcludedComponent(Component component) {<br \/>\n        components.add(component);<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    protected boolean accept(Component aComponent) {<br \/>\n        if (components.contains(aComponent)) {<br \/>\n            return false;<br \/>\n        }<br \/>\n        return super.accept(aComponent);<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p>And then in the form after adding the components call:<\/p>\n<p>[java]ExclusionFocusTraversalPolicy policy = new ExclusionFocusTraversalPolicy();<br \/>\n\/\/ add all the desired components to the exclusion list<br \/>\npolicy.addExcludedComponent(textField);<br \/>\nsetFocusTraversalPolicy(policy);<br \/>\nsetFocusTraversalPolicyProvider(true);[\/java]<\/p>\n<p>That is it. Now when tabbing through the form, all the components in the exclusion list are skipped!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I set out to accomplish something that appeared to be quite simple. However, it took me quite a few hours and lots of reading to find a solution. I do hope that this will be useful to other developers! I have been working a JPanel form with a lot of different components on it, &#8230; <a title=\"Exclude Components from Tab Order\" class=\"read-more\" href=\"https:\/\/www.pellissier.co.za\/hermien\/?p=610\" aria-label=\"More on Exclude Components from Tab Order\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[6],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1v8WL-9Q","_links":{"self":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/610"}],"collection":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=610"}],"version-history":[{"count":0,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}