How can we prevent JTabbedPanes from transferring focus to components outside of the tabs during tab traversal?

Hi,
I noticed a strange focus traversal behavior of JTabbedPane.
During tab traversal (when the user's intention is just to switch between tabs), the focus is transferred to a component outside of the tabs (if there is a component after/below the JTabbedPane component), if using Java 6. For example, if using the SSCCE below...
import java.awt.BorderLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TabbedPaneTest extends JPanel {
    public TabbedPaneTest() {
        super(new BorderLayout());
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Tab 1", buildPanelWithChildComponents());
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        tabbedPane.addTab("Tab 2", buildPanelWithChildComponents());
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
        tabbedPane.addTab("Tab 3", buildPanelWithChildComponents());
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
        tabbedPane.addTab("Tab 4", buildPanelWithChildComponents());
        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(tabbedPane);
        JButton button = new JButton("Dummy component that gains focus when switching tabs");
        panel.add(button, BorderLayout.SOUTH);
         * To replicate the focus traversal issue, please follow these steps -
         * 1) Run this program in Java 6; and then
         * 2) Click on a child component inside any tab; and then
         * 3) Click on any other tab (or use the mnemonic keys ALT + 1 to ALT 4).
        button.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                System.err.println("Gained focus (not supposed to when just switching tabs).");
        add(new JScrollPane(panel));
    private JPanel buildPanelWithChildComponents() {
        JPanel panel = new JPanel();
        BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
        panel.setLayout(boxlayout);
        panel.add(Box.createVerticalStrut(3));
        for (int i = 0; i < 4; i++) {
            panel.add(new JTextField(10));
            panel.add(Box.createVerticalStrut(3));
        return panel;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Test for Java 6");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TabbedPaneTest());
                frame.pack();
                frame.setVisible(true);
... Then we can replicate this behavior by following these steps:
1) Run the program in Java 6; and then
2) Click on a child component in any of the tabs; and then
3) Click on any other tab (or use the mnemonic keys 'ALT + 1' to 'ALT + 4').
At step 3 (upon selecting any other tab), the focus would go to the component below the JTabbedPane first (hence the printed message in the console), before actually going to the selected tab.
This does not occur in Java 7, so I'm assuming it is a bug that is fixed. And I know that Oracle suggests that we should use Java 7 nowadays.
The problem is: We need to stick to Java 6 for a certain application. So I'm looking for a way to fix this issue for all our JTabbedPane components while using Java 6.
So, is there a way to prevent JTabbedPanes from passing the focus to components outside of the tabs during tab traversal (e.g. when users are just switching between tabs), in Java 6?
Note: I've read the release notes between Java 6u45 to Java 7u15, but I was unable to find any changes related to the JTabbedPane component. So any pointers on this would be deeply appreciated.
Regards,
James

Hi Kleopatra,
Thanks for the reply.
Please allow me to clarify first: Actually the problem is not that the child components (inside tabs) get focused before the selected tab. The problem is: the component outside of the tabs gets focused before the selected tab. For example, the JButton in the SSCCE posted above gets focused when users switch between tabs, despite the fact that the JButton is not a child component of the JTabbedPane.
It is important for me to prevent this behavior because it causes a usability issue for forms with 'auto-scrolling' features.
What I mean by 'auto-scrolling' here is: a feature where the form automatically scrolls down to show the current focused component (if the component is not already visible). This is a usability improvement for long forms with scroll bars (which saves the users' effort of manually scrolling down just to see the focused component).
To see this feature in action, please run the SSCCE below, and keep pressing the 'Tab' key (the scroll pane will follow the focused component automatically):
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
public class TabbedPaneAutoScrollTest extends JPanel {
    private AutoScrollFocusHandler autoScrollFocusHandler;
    public TabbedPaneAutoScrollTest() {
        super(new BorderLayout());
        autoScrollFocusHandler = new AutoScrollFocusHandler();
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Tab 1", buildPanelWithChildComponents(20));
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        tabbedPane.addTab("Tab 2", buildPanelWithChildComponents(20));
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
        tabbedPane.addTab("Tab 3", buildPanelWithChildComponents(20));
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
        tabbedPane.addTab("Tab 4", buildPanelWithChildComponents(20));
        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(tabbedPane);
        JButton button = new JButton("Dummy component that gains focus when switching tabs");
        panel.add(button, BorderLayout.SOUTH);
         * To replicate the focus traversal issue, please follow these steps -
         * 1) Run this program in Java 6; and then
         * 2) Click on a child component inside any tab; and then
         * 3) Click on any other tab (or use the mnemonic keys ALT + 1 to ALT 4).
        button.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                System.err.println("Gained focus (not supposed to when just switching tabs).");
        button.addFocusListener(autoScrollFocusHandler);
        JScrollPane scrollPane = new JScrollPane(panel);
        add(scrollPane);
        autoScrollFocusHandler.setScrollPane(scrollPane);
    private JPanel buildPanelWithChildComponents(int numberOfChildComponents) {
        final JPanel panel = new JPanel(new GridBagLayout());
        final String labelPrefix = "Dummy Field ";
        final Insets labelInsets = new Insets(5, 5, 5, 5);
        final Insets textFieldInsets = new Insets(5, 0, 5, 0);
        final GridBagConstraints gridBagConstraints = new GridBagConstraints();
        JTextField textField;
        for (int i = 0; i < numberOfChildComponents; i++) {
            gridBagConstraints.insets = labelInsets;
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = i;
            panel.add(new JLabel(labelPrefix + (i + 1)), gridBagConstraints);
            gridBagConstraints.insets = textFieldInsets;
            gridBagConstraints.gridx = 2;
            textField = new JTextField(22);
            panel.add(textField, gridBagConstraints);
            textField.addFocusListener(autoScrollFocusHandler);
        return panel;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Test for Java 6 with auto-scrolling");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TabbedPaneAutoScrollTest());
                frame.setSize(400, 300);
                frame.setVisible(true);
* Crude but simple example for auto-scrolling to focused components.
* Note: We don't actually use FocusListeners for this feature,
*       but this is short enough to demonstrate how it behaves.
class AutoScrollFocusHandler extends FocusAdapter {
    private JViewport viewport;
    private JComponent view;
    public void setScrollPane(JScrollPane scrollPane) {
        viewport = scrollPane.getViewport();
        view = (JComponent) viewport.getView();
    @Override
    public void focusGained(FocusEvent event) {
        Component component = (Component) event.getSource();
        view.scrollRectToVisible(SwingUtilities.convertRectangle(component.getParent(),
                component.getBounds(), view));
Now, while the focus is still within the tab contents, try to switch to any other tab (e.g. by clicking on the tab headers, or by using the mnemonic keys 'ALT + 1' to 'ALT + 4')...
... then you'll notice the following usability issue:
1) JRE 1.6 causes the focus to transfer to the JButton (which is outside of the tabs entirely) first; then
2) In response to the JButton gaining focus, the 'auto-scrolling' feature scrolls down to the bottom of the form, to show the JButton. At this point, the tab headers are hidden from view since there are many child components; then
3) JRE 1.6 transfers the focus to the tab contents; then
4) The 'auto-scrolling' feature scrolls up to the selected tab's contents, but the tab header itself is still hidden from view (as a side effect of the behavior above); then
5) Users are forced to manually scroll up to see the tab headers whenever they are just switching between tabs.
In short, the tab headers will be hidden when users switch tabs, due to the Java 6 behavior posted above.
That is why it is important for me to prevent the behavior in my first post above (so that it won't cause usability issues when we apply the 'auto-scrolling' feature to our forms).
Best Regards,
James

Similar Messages

  • How can I prevent iTunes from opening when Firefox is open, when the computer is not used for a few minutes?

    iTunes opens on my screen if Firefox is on, with page minimised or with current page open, if I do not use the machine for a few minutes. I can't find anything to turn this off.

    When you first insert a CD your Mac comes up with a box saying
    "You have inserted a CD" (duuh, Apple...)
    Then you choose from a pop-up menu that offers "Open in Finder", "Open in iTunes", etc
    At some point you (or someone) has chosen "Open in iTunes" then checked the little box that says "Make this the default from now on".
    If you go into System Prefs (as already mentioned) you have a choice to reset what you want to do with a blank CD.

  • How can I prevent Firefox from re-installing test pilot?

    I had removed the test pilot extension and its feedback button in Firefox 4.0b9. I let Firefox upgrade itself to 4.0b10 and didn't notice that silently re-installed it.
    I was typing in another application when suddenly that window was minimized and the focus shifted to a Firefox "new test pilot study" window that popped up. Even if I had given permission for that software to be installed, that is rude.
    I am not willing to trust that if I disable this logging software that it will stay disabled the next time I upgrade. I will remove test pilot again. '''How can I prevent Firefox from re-installing it?'''
    One way might be to set extensions.rdf read-only. However, I do not wish to lose the ability to install add-ons, I merely do not want anybody (including Mozilla) to install an add-on without my permission.
    https://bugzilla.mozilla.org/show_bug.cgi?id=519357 doesn't seem to address this problem as test pilot is a known component. Its not a plug-in so I can't modify the plugin scanning settings to prevent it from being loaded. Is there some way I can reliably supplement http://www.mozilla.com/en-US/blocklist/ with a local block list file? If so, would I have to update that whenever a new version of test Pilot is released?
    http://support.mozilla.com/en-US/kb/Add-ons%20blocklist

    I'm not sure what you mean by "re-directs e-mails." Are the recipient addresses changing between the time you address the message and the time it is sent?
    If you could provide more information I think that would help in tracking this down.
    When you use Firefox to work with your webmail site, whether Yahoo! or Gmail or another, the mechanics of sending the mail are handled by the site itself. So if there is a problem with how messages are addressed, then you may need to check your Yahoo! mail settings.

  • My child accidentally rented a movie on Apple TV.  Is there a way to refund the rental if we don't watch it?  How can I prevent this from happening?  Is there a way to password protect future purchases?

    My child accidentally rented a movie on Apple TV.  Is there a way to refund the rental if we don't watch it? 
    How can I prevent this from happening?  Is there a way to password protect future purchases?

    Did you try Parental Controls?
    Apple TV: Understanding Restrictions (parental controls) - Apple Support
    See if that does the trick: I think there's a way for you to request a password for purchases, but still be logged in.

  • How can I prevent Bridge from altering my photos?

    HELP!!!
    Within the past few days my Bridge CS4 program has been making my life miserable. I had been utilizing the program to batch photos from my extensive photo library, rename them, sort them (i.e., change sort/ordinal number), and the like without any difficulty and then suddenly, after executing such processes my thumbnails turned completely white and would not open in Bridge, Photoshop CS4 Extended - much less anything else. I tried reopening them as .jpg's, .psd's, .tiff's to no avail. I went into properties on each photo and checked the "open with" tab, and it noted that the photo was not recognized. I had some moderate success opening them with Quicktime Picture Viewer, but converting them back to a Bridge readable format (i.e., their original state) proved impossible. Having noticed that Quicktime had hijacked nearly my entire photo opening process, I uninstalled it in the hope that it would solve the problem. It didn't. I then went into Bridge and Photoshop CS4 preferences to try and modify how photos were opened, and couldn't seem to find anything applicable. I did, however, increase the memory and cache allocated to each program substantially.
    Last night I ran into more problems with Bridge. While scanning both photo negatives and prints into Bridge and saving them into various files, I encountered the same problem indicated in paragraph one. I tried scanning several of them a second time and succeeded; however, I had to save them to a different file and give them dissimilar names in .tiff format. In the process I found more than a dozen ways to spell "Mexico," "Tokyo," and "Ukraine"! They would not save as .jpg's or .psd's.
    I use Windows Vista Home Premium SP1, have a Pentium 4 Dual Core (2.8) processor, 4 GB Memory, Nvidia GE Force 7600 video card (yes, the drivers are up to date), and Hewlett Packard Photosmart C7180 and C7280 printers for scanning. All of the photos involved are stored on a Maxtor One Touch 1TB Turbo auxillary hard drive.
    Also, could anyone suggest how I can get the  SD/MMC card reader on my PC to work?
    Thanks to any and all for assisting me with my problem.
    Wolfgang Holst
    Big Bear Lake, CA

    Thank you curt y for your quick response. I have seen your recommendations on the adobe forums link in the past and many of your solutions
    seem to have helped other people with Bridge problems. I took your suggestion and gave it a whirl - all preferences were restored to defaults - and the problem continues. I get the feeling the problems lies with the program's interaction with Windows.
    Having tried a few more things, I've found that I can move the photo shell (the inscription underneath the thumbnail states, "Window Shell Common") to my desktop and open it there with Photoshop (i.e., going through the song and dance of "open with" and clicking on the CS4 icon, where it opens up in RAW). I must then move it over to Photoshop CS4 and save it as a .jpg or a .psd. Unfortunately, after all this it still won't open in Bridge. By all appearances, Bridge somehow has become corrupted and I'm probably going to have to do a complete Photoshop CS4 uninstall and reinstall.
    I've spent hundreds - if not thousands - of hours scanning into my system some 10,000 photographs of my travels (yes, they are backed up on discs), and Bridge should not be negatively impacting my ability to organize and manipulation them as is happening now. If the uninstall/reinstall does not resolve the problem, I'll be back on this forum for more suggestions.
    As for the card reader issue, I'm well aware of the 2GB/over 2GB issue. I typically use a 4GB or more card in my camera. I can download fine through my HP 7180 and 7280 card readers, but my HP 1518n card reader is a different story. I'll work a little more on it, too.
    Thanks again.
    Wolfgang Holst
    Big Bear Lake, CA    
    Date: Thu, 18 Jun 2009 08:49:46 -0600
    From: [email protected]
    To: [email protected]
    Subject: How can I prevent Bridge from altering my photos?
    Have you tried resetting your preferences by holding down the Ctrl key and starting Bridge?  You will get a reset window, choose all 3 options.
    The only thing I know about card readers is that there are 2 versions.  One for cards of 2 gigs or less, and one for HD cards over 2 gigs.  Make sure card and reader match.
    >

  • How can i prevent iphoto from opening when i plug in my iphone to the computer?

    Hi There,
    I have  a question, when I connect my iPhone to the computer the iPhoto automatically opens.
    how can i prevent this from happening? it always slows down my computer.
    any help will be appreciated.
    thanks

    Image Capture (in your Applications Folder) - In the preferences you can decide to 'Do Nothing' when a camera is connected.
    Some versions also have this option in the iPhoto Preferences.
    Regards
    TD

  • How can I prevent Firefox from cleaning my clipboard at exit (exit or closing firefox) ?

    How can I prevent Firefox from cleaning my clipboard at exit (exit or closing firefox) ?
    I am using FF 9.0.1 and the problem start from FF 5.0

    You can't prevent that.<br />
    If you've placed data on the clipboard in Firefox then you need to paste that data in the other program, if that is your intention, before closing Firefox.

  • I receive numerous messages in "Bulk Mail". Where do they come from and how can I prevent them from being received? Thanks

    I receive numerous messages in "Bulk Mail". Where do they come from and how can I prevent them from being received? Thanks

    Once you're on a spammer's list, there's nothing you can do to stop the flood of e-mail messages. You can either control it, using spam filtering (which is what files those messages in the Junk Mail folder), or you can throw out your e-mail address and get a new one.
    It's sad, but spam is simply a fact of life at this point, and there's nothing to be done about it. Legislation has been tried, with zero success, since most spam either comes from countries with no legislation or from personal computers that have been infected with malware and are part of a "botnet."

  • How can I prevent oracle from locking accounts after failed logins?

    how can I prevent oracle from locking accounts after failed logins?
    Thanks

    svarma wrote:
    So what is the difference between the profile settings ...FAILED_LOGIN_ATTEMPTS and the parameter settings SEC_MAX_FAILED_LOGIN_ATTEMPTS?
    Prior to 11g we only used profiles to control failed_login_attempts.. Then why we need thsi new parameter now?http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/initparams221.htm#I1010274
    http://download.oracle.com/docs/cd/E11882_01/server.112/e17222/changes.htm#UPGRD12504
    http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_6010.htm#SQLRF01310
    As documented ...
    FAILED_LOGIN_ATTEMPTS is a property of a profile, and will lock an account
    SEC_MAX_FAILED_LOGIN_ATTEMPTS is an initialization parameter and will drop a connection but says nothing about locking accounts.

  • I have been trying to work on my homework all afternoon, via NAU's blackboard system. I am continuously getting booted out of the system, with an error stating "Data execution prevention". How can I prevent this from continuing?

    I have been trying to work on my homework all afternoon, via NAU's blackboard system. I am continuously getting booted out of the system, with an error stating "Data execution prevention". How can I prevent this from continuing?

    If you are wondering why you are not getting any responses, it is because you have vented a complaint without any details that make any sense or give anyone something to work on.
    If you want help, I suggest actually detailing what has happened, with versions of software etc. Anything that would let us assist.
    As a start I am guessing that you have not really got the hang of "How it all works". Firstly download the Pages09_UserGuide.pdf from under the Help menu. Read that and view the Video Tutorials in the same place. A good addition would be the iWork 09 Missing manual book and something to help you learn how to use your Mac.
    If there are specific tasks you need help with:
    http://www.freeforum101.com/iworktipsntrick/index.php?mforum=iworktipsntrick
    Is a good resource.
    Peter

  • How can I stop iTunes from transferring purchased apps from iPad to computer?

    How can I stop iTunes from transferring purchased apps from iPad to my computer? I don't want them taking up space on my computer. iPad backups and apps on the cloud are enough to ensure my apps are backed up, so I don't understand why they need to be on iTunes too. I'm running Mavericks, iTunes 11.1.5 and iOS 7.0.6.

    There isn't an app for that!
    As far as I can tell there is no option to give you that control. It is worth noting that apps can be withdrawn from the store at any time. Should your device need restoring you need all the apps in your library to transfer to it before the app data can be successfully restored. Pulling that all down from the cloud would be time consuming.
    If space is becoming an issue you should consider moving the library to a bigger drive (which you should also backup to another).
    As a temporary measure you could try deleting some larger apps using Finder from within iTunes/iTunes Media/Mobile Applications while keeping the entries in your library. iTunes may complain during syncs that it cannot copy apps to the device because they cannot be found, but it should not clear them from the device. Test first on a single app that doesn't have any data that you would mind losing in case the behavior has changed from the last time I tested it.
    tt2

  • I use to administrate my DSL modem via an ip-address. When I enter it into FF8 I am asked where to save the file. Why and how can I prevent FF8 from doing that?

    I use to administrate my DSL modem via an ip-address. When I enter it into FF8 I am asked where to save the file. Why and how can I prevent FF8 from doing that?
    And now anytime I am entering an ip-address I am asked if I want to download the file.

    It happens when the modem server doesn't send the file as text/html, but with another MIME type.<br />
    I tried the index.html addition in case the server might send that file as text/html.<br />
    If your DSL modem has a support website then you can try to ask there for advice about how to configure the modem server.

  • HT6006 I was charged automatically for an App I don't even use. How can I prevent that from happening again?

    I really like my iTunes account, however I was automatically charged for an app that I don't use. How can I prevent that from happening again?

    Sew Ida wrote:
    I was automatically charged for an app that I don't use.
    Please explain what happened.

  • How can I make albums from events then delete events without deleting the album?

    Hey, just need a little help!
    How can I make albums from events then delete events without deleting the album?
    Many thanks

    You'll be more likely to get help with this if you ask in the iPhoto forum:
    https://discussions.apple.com/community/ilife/iphoto
    You'll need to tell people what version of iPhoto you have so they can give you correct advice.
    Regards.

  • How can I delete photos from my iPad, iPhone, and laptop at the same time?

    How can I delete photos from my iPad, iPhone, and laptop at the same time so I don't have to do it separately on each device?

    Photos that were synced from your computer using iTunes can only be deleted by de-selecting them from the folder from where they were synced -  and then re-syncing.
    Connect the iPad to the PC and launch iTunes.
    Click on the iPad name on the left side under devices.
    Click on the Photos Tab on the right.
    If you want to remove all of the Photos - uncheck the Sync Photos from Heading
    Click on Apply in the lower right corner of iTunes
    If you are using iTunes 11 - this will be helpful, Setting up syncing in Windows.
    http://support.apple.com/kb/PH12313

Maybe you are looking for