Java Tips Weblog

  • Blog Stats

    • 2,065,823 hits
  • Categories

  • Archives

  • Advertisements

Tabbed Pane Focus

Posted by Rob Camick on October 30, 2008

It seems to me that the focus policy of JTabbedPane has changed from JDK1.4.2 to the current JDK6 version.

The current version remembers which component last had focus on a tab and will restore focus to that component again when the tab is reselected. In earlier versions the component with focus was not remembered and focus was placed on the first component on the tab each time a tab was reselected.

Both focus policies are valid. It would be nice to have control over which focus policy you want to use independent of the JDK version you are using.

The TabFocusHandler class will allow you to choose one of the following focus policies:

  • Reset Focus – focus will always be placed on the first component on a tab when the tab is selected
  • Retain Focus (the default) – focus will be placed on the component that last had focus on the tab. If this is the first time selecting the tab, then the first component will receive focus.

In addition the TabFocusHandler will allow you to have a combination of focus policies. You specify the default policy from one of the options above. Then you can exclude specific tabs from that policy in which case the other policy will be used. For example, you can set the tabbed pane to retain focus on all tabs except the first tab using the following code:

TabFocusHandler fh = new TabFocusHandler(tabbedPane);

In summary, the TabFocusHandler allows you to revert to the old focus policy (Reset) on new JDKs or you can upgrade an older JDK to use the new focus policy (Retain).

Get The Code

Related Reading

How to Write a Change Listener
How to Write a Property Change Listener


2 Responses to “Tabbed Pane Focus”

  1. Marco said

    Is the put(key, null) necessary in stateChanged?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: