Open JTabbedPane with focus in tab

I have a swing standalone app where general navigation is managed with something like this:
   private void addNewComponent(JComponent component) {
      JComponent panel = main.getDesktopComponent();
      main.setMainTittle(GlobalOptions.getTittle());
      panel.removeAll();
      panel.add(component, BorderLayout.CENTER);
      component.requestFocus();
      panel.validate();
      panel.repaint();
   } //EOF addNewComponentOne of this changed central panel's components is a JTabbedPane
This pane has 3 tabs, each with full tree of containers and components.
The problem is that when central app panel shows the JTabbedPane, the only way to focus in a component is by mouse click, after that i can use tab key to switch between components, including tabs from pane.
I cant find a way to force focus so mouse click is not necesary.
The component parameter in previous code has been a container in any part of app code I've seen it, so far, somewhere in each of this containers is a back button to mantain consistent the navigation. But I need to keep anytime the tab key navigation.
Also while tab key is not working sometimes hot keys neither work, so fast keyboard navigation crashes completely.
Is there a way I can fix this? tabs in JTabbedPane cant be selected as components, requestFocus in other components doesnt work, searching in the net shows posts with similar problems but says that it's a bug and need to change to jdk 1.5, I prefer to keep 1.4.2 version at least for a while since app is spreaded to many people and i dont control that.
Thanks in advance for the help.

You need to look into the new focus system. Focus always defaults the root of the default FocusTraversalPolicy. You need to define a policy. Then when the window opens focus will default to the tab if defined correctly.

Similar Messages

  • JTabbedPane - traversing focus through tabs

    I would like to have focus traverse through tabs in a JTabbedPane. That is, when the focus is on the last component of a tab, hitting the key for forward focus traverse should bring up the next tab and focus on the first component on that tab. I'm already using a custom FocusTraversalPolicy on the panel containing the JTabbedPane, which handles focus traversal between the components on the tabs. This policy has no knowledge of which tab a particular component is on.
    I was hoping calling requestFocusInWindow() would automagically focus that tab, but it doesn't. Needless to say, having the focus traversal policy cycle through all the components (including those in non-selected tabs) doesn't work, either. Is there an elegant way to do this, without resorting to listening to key events, watching for the TAB key, and switching to the appropriate tab?

    Ok. here is my suggestion.
    When the focus is going to be transferred to the other tab use tabpane.setSelectedComponent(nextPanel), it will trigger traverse policy once again, but we will catch the fact that current component is JPanel and transfer it further to our field.
    Following code looks ugly:
    order.add(tf1);
    order.add(tf2);
    parentsForward.put(p,tf1);
    parentsBackward.put(p,b2);but this is a concept demonstration only, you can hide this piece inside TraversalPolicy implementation.
    Here is the full code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    public class TabbedFocusExample extends JFrame  {
        public TabbedFocusExample()  throws HeadlessException {
            super("Tabbed Focus Test");
            setLayout(new FlowLayout());
            JPanel p;
            final JTabbedPane tp = new JTabbedPane();
            final JTextField tf1 = new JTextField(20);
            final JTextField tf2 = new JTextField(20);
            final JCheckBox cb1 = new JCheckBox("Option 1");
            final JCheckBox cb2 = new JCheckBox("Option 2");
            final JButton b1 = new JButton("Click me!");
            final JButton b2 = new JButton("Press me!");
         //stores real components, no container
         Vector<Component> order = new Vector<Component>();
         //stores (container,first_component_in_this_container) pair
            Map<Component,JComponent> parentsForward = new HashMap<Component,JComponent>();
         //stores (container,last_component_in_previous_container) pair
         Map<Component,JComponent> parentsBackward = new HashMap<Component,JComponent>();
            p = new JPanel(new FlowLayout());
            p.add(tf1);
            p.add(tf2);
            tp.addTab("Tab 1",p);
         order.add(tf1);
            order.add(tf2);
         parentsForward.put(p,tf1);
         parentsBackward.put(p,b2);
            p = new JPanel(new FlowLayout());
            p.add(cb1);
            p.add(cb2);
            tp.addTab("Tab 2", p);
         order.add(cb1);
            order.add(cb2);
         parentsForward.put(p,cb1);
         parentsBackward.put(p,tf2);
            p = new JPanel(new FlowLayout());
            p.add(b1);
            p.add(b2);
            tp.addTab("Tab 3", p);
         order.add(b1);
            order.add(b2);
         parentsForward.put(p,b1);
         parentsBackward.put(p,cb2);
         setFocusTraversalPolicy(new MyOwnFocusTraversalPolicy(order,parentsBackward,
                              parentsForward,tp));
            add(tp);
            pack();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        public static void main(String[] args) {
            new TabbedFocusExample().setVisible(true);
        public  class MyOwnFocusTraversalPolicy     extends FocusTraversalPolicy   {
         private Vector<Component> order;
         private JTabbedPane tabbedPane;
         private Map<Component, JComponent> parentsForward;
         private Map<Component, JComponent> parentsBackward;
            public MyOwnFocusTraversalPolicy(Vector<Component> order,Map<Component,JComponent> parentsF,
                                       Map<Component,JComponent> parentsB,JTabbedPane jtb) {
                this.order = order;
             this.tabbedPane=jtb;
             this.parentsForward = parentsF;
                this.parentsBackward = parentsB;
            public Component getComponentAfter(Container focusCycleRoot,
                                               Component aComponent) {
              JComponent nextComp = parentsForward.get(aComponent);
              if(nextComp!=null){ //aComponent is Container return first component in this Container
                   return nextComp;
                    int idx = (order.indexOf(aComponent) + 1) % order.size();
              nextComp = (JComponent)order.get(idx);
              int indexCurrent = tabbedPane.indexOfComponent(((JComponent)aComponent).getParent());
              int indexNext = tabbedPane.indexOfComponent(nextComp.getParent());
              if(indexNext!=indexCurrent){ //if next Component sits in next tab go to next tab
                   tabbedPane.setSelectedComponent(nextComp.getParent());
                    return nextComp;
         //same stuff but in opposite direction
            public Component getComponentBefore(Container focusCycleRoot,
                                                Component aComponent)  {
              JComponent prevComp= parentsBackward.get(aComponent);
              if(prevComp!=null){
                   return prevComp;
                    int idx = order.indexOf(aComponent) - 1;
                    if (idx < 0) {
                          idx = order.size() - 1;
                    prevComp = (JComponent)order.get(idx);
              int indexCurrent = tabbedPane.indexOfComponent(((JComponent)aComponent).getParent());
              int indexPrevious = tabbedPane.indexOfComponent(prevComp.getParent());
              if(indexPrevious!=indexCurrent){
                   tabbedPane.setSelectedComponent(prevComp.getParent());
                   return prevComp;
            public Component getDefaultComponent(Container focusCycleRoot) {
                return order.get(0);
            public Component getLastComponent(Container focusCycleRoot) {
                return order.lastElement();
            public Component getFirstComponent(Container focusCycleRoot) {
                return order.get(0);
    }

  • Option in safari to open links with a new tab has stopped working.

    This has happened in the last week.
    file://localhost/Users/barobins/Desktop/Screen%20Shot%202013-01-26%20at%2012.36. 34%20PM.png

    Uninstall the Ask toolbar and it should work again. There is a compatibility issue with the Ask toolbar and Firefox that prevents new tabs from being opened.

  • JTabbedPane with spacers between tabs

    I've searched everywhere but i cant seem to find an answer. This one is a toughie.
    I have extended the BasicTabbedPaneUI with a custom UI, everything looks good so far, but there is one major obstacle that i'm trying to overcome. I need to add spaces between my tabs.
    How can this be done ?
    This is the look that i am trying to achieve.
    ____________| |_| |__| |_________________
    I appreciate all inputs. Thank You.

    lets try that again
    ____|                       |____|                      |____well. i hope you get my point?

  • Upon opening iTunes app on either my iPad or PC it automatically opens the iTunes store and not the opening page with the 'summary' tab....help!,,,

    Upon opening either my PC or IPad to ITunes app screen goes automatically to ITunes store....
    I need to get to page that has summary tab on it....Help!,,,

    On your PC...
    Connect the Device... open iTunes, click on the Device tab that appears in the Left side column of iTunes, Select your Device,  the Summary Page should appear.

  • JTabbedPane, how can a tab switch to another tab?

    I have a JTabbedPane with a few tabs. Each tab consists of a class extending JPanel. If a certain action is performed on on of the tabs, the view should switch to another tab. How can this be done? I know about the method setSelectedComponent in the JTabbedPane class. But since the tab doesn't know about this, how can I use it?

    Actually, the solution was dead simple. All I had to do was give the constructor of the class/tab a reference to the JTabbedPanel instance so it can call setSelectedIndex on it.
    It works now, thanks anyway.

  • With a new tab, I get a black screen under the search bar with overlapping words of "Editor sites" & "edit" & "search". Change it how?" on the bottom.

    There is also a search box that appears in the middle of the black screen. Not a regular search, but its return gave "suggestions" instead. The left bottom of the screen has like a double exposure of the aforementioned, overlapping words. If I hit the Home button I will get the Firefox search screen. But I can't pin even a normal search screen. I have no idea why this started to happen, but I think it began with the latest update to Firefox I did a few days ago. How can I get back to what used to open up with a new tab?
    Thank you for you help.

    You can use the SearchReset extension to reset some preferences to the default values.
    *https://addons.mozilla.org/firefox/addon/searchreset/
    Note that the SearchReset extension only runs once and then uninstalls automatically, so it won't show on the "Firefox > Add-ons" page (about:addons).
    Start Firefox in <u>[[Safe Mode|Safe Mode]]</u> to check if one of the extensions (Firefox/Tools > Add-ons > Extensions) or if hardware acceleration is causing the problem.
    *Switch to the DEFAULT theme: Firefox/Tools > Add-ons > Appearance
    *Do NOT click the Reset button on the Safe Mode start window
    *https://support.mozilla.org/kb/Safe+Mode
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes

  • Problem with new firefox tabs and window focus

    Here's a problem that I've had for ages - sometimes it annoys me, other times I can live with it. Right now it's really p***ing me off.  :x
    I have selected 'a new tab in the most recent window' under FF's Preferences->Tabs->Open links from other applications in:, so as an example, if I click on a URL in a mail in Thunderbird, Firefox opens it in a new tab. However, window focus stays on Thunderbird. I run both of them full screen, so although I'm looking at the web page in Firefox, anything I do on the keyboards affects Thunderbird. It affects URLs opened from other apps too e.g. if I open a terminal and do
    firefox www.archlinux.org
    focus stays on the terminal window. The only exception is when firefox is not already running - in that case, firefox launches with focus.
    DE is xfce4, and the system is completely up to date.
    I've done some googling, and AFAICS, nobody else seems to have this problem. Any ideas, anyone?
    TIA.

    Thanks MAC!EK - definitely a useful add-on, but it still doesn't solve my problem. In Tab Mix Plus Options, I have the following selected:
    Links -> Open links from other applications in: New tab
    Events-> Tab Focus -> Focus/Select tabs  that open from: <all>
    However, the behaviour is as before i.e. I click a URL in Thunderbird, it opens in a new tab in Firefox, but window focus stays on Thunderbird. Within Firefox, the newly-opened tab is selected or focussed, but Firefox itself is not.
    Anyway, thanks again. I think I need to keep looking.

  • Hello..i use firefox 4 beta 05..my question is..why when i open let's say 6 tabs..my windows show that i have 6 instancies of firefox instead of one like it does with firefox 3.6? thanks

    hello..i use firefox 4 beta 05..my question is..why when i open let's say 6 tabs..my windows show that i have 6 instancies of firefox instead of one like it does with firefox 3.6? thanks
    maybe i am saying it wrong..when i press alt+tab there is only one firefox open..but in windows 7 in the taskbar..every tab open..appears as another window open

    If you are referring to taskbar previews, you can turn them off by modifying a hidden preference.
    # Type '''about:config''' into the location bar and press enter
    # Accept the warning message that appears, you will be taken to a list of preferences
    # In the filter box type '''previews'''
    # Double-click on the preference browser.taskbar.previews.enable to change its value to '''false'''

  • How can I stop multiple tabs from opening along with Mozilla Firefox Start Page at launching time.How can this be stopped?

    Whenever I launch Firefox, multiple tabs also start to open along with the Mozilla Firefox Start Page, which is very irritating. How can I stop this to happen each time I launch the browser?

    See these articles for some suggestions:
    * https://support.mozilla.com/kb/Firefox+has+just+updated+tab+shows+each+time+you+start+Firefox
    * https://support.mozilla.com/kb/How+to+set+the+home+page - Firefox supports multiple home pages separated by '|' symbols
    * http://kb.mozillazine.org/Preferences_not_saved
    It is also possible that there is a problem with the files sessionstore.js and sessionstore.bak in the Firefox Profile Folder.
    Delete the files sessionstore.js and sessionstore.bak in the Firefox Profile Folder.
    * Help > Troubleshooting Information > Profile Directory: Open Containing Folder
    * http://kb.mozillazine.org/Profile_folder_-_Firefox
    * http://kb.mozillazine.org/sessionstore.js
    If you see files sessionstore-##.js with a number in the left part of the name like sessionstore-1.js then delete those as well.<br />
    Deleting sessionstore.js will cause App Tabs and Tab Groups to get lost, so you will have to create them again (make a note).
    See:
    * http://kb.mozillazine.org/Session_Restore

  • How do i get a new tab to open up with my home page after i click on the + at the end of the tab

    how do i get a new tab to open up with my home page after i click on the + at the end of the tab

    problem solved thak you Andy.c that was too easy and a fast reply i'm abit of an iliterate with these things

  • Why does a new tab page open along with my home page?

    When I go online, I expect to have only my home page open, instead, I have two tabs, my home page tab and New Tab and the New Tab page is the page that actually opens. This just started yesterday, why??

    Did you check the home page setting to make sure that it is still correct?
    *Tools > Options > General > Startup: Home page
    *https://support.mozilla.org/kb/How+to+set+the+home+page
    Firefox supports multiple home pages separated by '|' (pipe) symbols.
    You can check the target line in the Firefox desktop shortcut (right-click: Properties) to make sure that nothing is appended after the path to the Firefox program.
    You can check for problems with preferences.
    *http://kb.mozillazine.org/Preferences_not_saved
    *http://kb.mozillazine.org/Resetting_preferences

  • How to change the page that opens with a new tab to the default thumbnail screen?

    Some nefarious add-on appeared, so I deleted it. But I'm still left with it's search engine as my new tab page, instead of the default page with the thumbnails, which I infact really like.
    This has happened before, and I don't know how to get rid of it without resetting Firefox and losing all of my bookmarks (I know I can back them up), cache and other shizzle.
    So, how do I restore my new tab page to default?

    *1<BR>
    '''''Download the [https://addons.mozilla.org/en-US/firefox/addon/searchreset/ Mozilla Search Reset]'''''<BR><BR>
    This add-on is very simple: on installation, it backs up and then resets
    your search preferences and home page to their default values,
    and then uninstalls itself. This affects the search bar, URL bar
    searches, and the home page.<BR><BR>
    *2<BR>
    Open a new window or tab. In the address bar, type '''''about:config'''''.
    If a warning screen comes up, press the '''''Be Careful''''' button.
    This is where Firefox finds information it needs to run.
    At the top of the screen is a search bar. Enter '''''browser.newtab.url'''''
    and press enter. '''''browser.newtab.url'''''
    tells Firefox what to show when a new tab is opened.
    If you want, right click and select '''''Modify'''''. You can change the
    setting to;<BR><BR>about:home (Firefox default home page),<BR>
    about:newtab (shows the sites most visited),<BR>
    about:blank (a blank page),<BR>
    or you can enter any web page you want.<BR><BR>
    The same instructions are used for the new window setting, listed as
    '''''browser.startup.homepage'''''.

  • In Firefox 5 links with target="_blank" don't open in new window or tab. Is there a way to make them do that?

    I've search some info about this and it seems like there's a lot of people that are annoyed by links opening in new window/tab and that the 'target="_blank"' is not valid in strict XHTML. I'm guessing this could be reasons why Firefox 5 ignores the 'target="_blank"'. But in some cases you really want to open a new widow, in my case I'm using a flash application and want to open links in new windows from there and then the ctrl-click doesn't work. When opening the link in the same page and then go back the application is reloaded and it's state is lost. Probably should try to find out if there's some other way to open links from a flash application, but I'm wondering if there's a way to make Firefox 5 open 'target="_blank"' links in new tabs.

    I don't know how to delete my question, anyway I'm not having this problem anymore. In our organisation the IT department deploy Firefox updates with certain settings and limitations, in the tap-options the checkbox for opening new windows in new tabs where checked but disabled. They made some changes to make this checkbox enabled and now it works as expected if it's check new windows open i new tabs and if it's not in new windows. They never opens in the same tab as they did before.

  • Firefox always opens with the "search" tab even though I don't want it. How do I stop this?

    When I open Firefox, it opens normally to my home page. But it also opens with the "search" tab, though I don't want it. How do I prevent this?

    From the search page offered go to your chosen home page. Now move the mouse to the top left and scan the options for view, Toolbars and make sure that the "Menu bar" is ticked. This will show the File, Edit, View, History, Bookmarks and tools options plus Help. Click on tools, then Options and select current page as your home page. All should now be sorted.

Maybe you are looking for

  • Purchase price

    Dear All I have requirment My user want to know Stock avilability wtih Purchase Price. Is there any Possiblity Pl let me know regards

  • How to debug bulk insert?

    I have this code which doesn't cause any error, and actually gives message 'query executed successfully', but it doesn't load any data. bulk insert [dbo].[SPGT] from '\\sys.local\london-sql\FTP\20140210_SPGT.SPL' WITH ( KEEPNULLS, FIRSTROW=5, FIELDTE

  • Firefox displays gmail in huge font but other web sites show it in regular fonts

    when I open gmail in Firefox it has just started coming up in huge fonts so that i can only see part of the screen. When I open gmail in Internet Explorer or AOL, it comes up just fine and I can see the whole screen. How can I restore the Firefox dis

  • Why is the input that the Apple TV HDMI is hooked into disbaled?

    I plugged my Apple TV power cord in, and the HDMI cored into my TV. I tried hitting Input on my TV, but the only things it allows me to go in is TV, DVD, and Sattelite. The HDMI was plugged into Input or HDMI 7. Why is that disabled and how should I

  • Images not appear when using SUN IDE. help !

    I have the Sun IDE, and I have a problem. The problem is : when I compile using this IDE, Images do not appear. But when I compile using another IDE such as JPadPro, images appear well. Thank you !!