<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NetBeans Ruminations &#187; Swing</title>
	<atom:link href="http://www.pellissier.co.za/hermien/?cat=6&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.pellissier.co.za/hermien</link>
	<description>A NetBeans blog by Hermien Pellissier</description>
	<lastBuildDate>Wed, 24 Apr 2013 09:06:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Exclude Components from Tab Order</title>
		<link>http://www.pellissier.co.za/hermien/?p=610</link>
		<comments>http://www.pellissier.co.za/hermien/?p=610#comments</comments>
		<pubDate>Fri, 16 Nov 2012 07:01:51 +0000</pubDate>
		<dc:creator>Hermien Pellissier</dc:creator>
				<category><![CDATA[Swing]]></category>

		<guid isPermaLink="false">http://www.pellissier.co.za/hermien/?p=610</guid>
		<description><![CDATA[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, <a href='http://www.pellissier.co.za/hermien/?p=610' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<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>
<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>
<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>
<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>
<pre>public class ExclusionFocusTraversalPolicy 
        extends LayoutFocusTraversalPolicy {

    private ArrayList components = new ArrayList();

    public void addExcludedComponent(Component component) {
        components.add(component);
    }

    @Override
    protected boolean accept(Component aComponent) {
        if (components.contains(aComponent)) {
            return false;
        }
        return super.accept(aComponent);
    }
}</pre>
<p>And then in the form after adding the components call:</p>
<pre>ExclusionFocusTraversalPolicy policy = new ExclusionFocusTraversalPolicy();
// add all the desired components to the exclusion list
policy.addExcludedComponent(textField);
setFocusTraversalPolicy(policy);
setFocusTraversalPolicyProvider(true);</pre>
<p>That is it. Now when tabbing through the form, all the components in the exclusion list are skipped!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pellissier.co.za/hermien/?feed=rss2&#038;p=610</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
