Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6294949

Can't give Focus to desired compont inside JTabbedPane

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.5.0_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      When the user selects a tab i want to give the focus to a special component.
      So i registered a ChangeListener to the TabbedPane.
      In all version prior 1.5.0_04 it was working most of the time (i always had problems when using key listener to change the selected tab, like in the example, and than giving focus to a special component. This sometimes worked and sometimes not !) .
      Now Java seems to give the Focus always to the first componet in the tab.
      I think Its because of this bugfix:
      http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6210088.





      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run my src.
      Change Tabs.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When the user selects Tab 1(via mouse or via F1 or via Alt+1) focus should go to tf2, when the user select Tab 2 (via mouse or via F2 or via Alt+2) focus should go to tf4
      ACTUAL -
      Now focus always goes to tf1 or tf3.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import javax.swing.event.ChangeListener;
      import javax.swing.event.ChangeEvent;
      import java.awt.*;
      import java.awt.event.KeyAdapter;
      import java.awt.event.KeyEvent;

      public class TabbedPaneDemo extends JPanel {
          JTextField tf1;
          JTextField tf2;
          JTextField tf3;
          JTextField tf4;
          JTabbedPane tabbedPane;
          KeyAdapter ka;

          public TabbedPaneDemo() {
              super(new GridLayout(1, 1));
              ka = new KeyAdapter() {
                  public void keyReleased(KeyEvent ev) {
                      if (ev.getKeyCode() == KeyEvent.VK_F1) {
                          tabbedPane.setSelectedIndex(0);
                          //tf2.requestFocusInWindow();
                      } else if (ev.getKeyCode() == KeyEvent.VK_F2) {
                          tabbedPane.setSelectedIndex(1);
                          //tf4.requestFocusInWindow();
                      }
                  }
              };
              tf1 = new JTextField();
              tf1.setColumns(10);
              tf1.addKeyListener(ka);
              tf2 = new JTextField();
              tf2.setColumns(10);
              tf2.addKeyListener(ka);
              tf3 = new JTextField();
              tf3.setColumns(10);
              tf3.addKeyListener(ka);
              tf4 = new JTextField();
              tf4.setColumns(10);
              tf4.addKeyListener(ka);


              tabbedPane = new JTabbedPane();
              tabbedPane.addChangeListener(new ChangeListener() {
                  public void stateChanged(ChangeEvent e) {
                      if (tabbedPane.getSelectedIndex() == 0) {
                          tf2.requestFocusInWindow();
                      } else {
                          tf4.requestFocusInWindow();
                      }
                  }
              });
              tabbedPane.setFocusable(false);
              JComponent panel1 = makeTextPanel("Panel #1");
              panel1.add(tf1);
              panel1.add(tf2);
              tabbedPane.addTab("Tab 1", null, panel1,
                                "Does nothing");
              tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

              JComponent panel2 = makeTextPanel("Panel #2");
              panel2.add(tf3);
              panel2.add(tf4);
              tabbedPane.addTab("Tab 2", null, panel2,
                                "Does twice as much nothing");
              tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

              add(tabbedPane);
          }

          protected JComponent makeTextPanel(String text) {
              JPanel panel = new JPanel(false);
              JLabel filler = new JLabel(text);
              filler.setHorizontalAlignment(JLabel.CENTER);
              panel.setLayout(new FlowLayout());
              panel.add(filler);
              return panel;
          }

          private static void createAndShowGUI() {
              JFrame frame = new JFrame("TabbedPaneDemo");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JComponent newContentPane = new TabbedPaneDemo();
              newContentPane.setOpaque(true);
              frame.getContentPane().add(new TabbedPaneDemo(),
                                       BorderLayout.CENTER);
              frame.pack();
              frame.setVisible(true);
          }

          public static void main(String[] args) {
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      createAndShowGUI();
                  }
              });
          }
      }
      ---------- END SOURCE ----------
      ###@###.### 2005-07-08 12:09:01 GMT

            shickeysunw Shannon Hickey (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: