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

JComponent subclasses do not get focus unless they call registerKeyboardAction

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • client-libs
    • beta2
    • x86
    • windows_nt



      Name: mt13159 Date: 07/20/2001


      Run the following application. Notice that you can only tab to the
      JComponent subclass that has registered a keyboard action. You should
      be able to tab to both components.

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class JTabOrderTest extends JFrame implements ActionListener
      {
          public static void main(String[] args)
          {
              JTabOrderTest test = new JTabOrderTest();
              test.setSize(100,100);
              test.setVisible(true);
          }

          public JTabOrderTest()
          {
              getContentPane().setLayout(new BorderLayout());

              Comp c1 = new Comp();
              KeyStroke s = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0);
              c1.registerKeyboardAction(this, "BUG", s, JComponent.WHEN_FOCUSED);

              getContentPane().add(BorderLayout.WEST, c1);
              getContentPane().add(BorderLayout.EAST, new Comp());
          }

          public void actionPerformed(ActionEvent e)
          {

          }

          private class Comp extends JComponent implements FocusListener
          {
              public Comp()
              {
                  addFocusListener(this);
              }

              public void paint(Graphics g)
              {
                  if (hasFocus())
                      g.setColor(Color.black);
                  else
                      g.setColor(Color.gray);

                  g.drawString("abc", 10, 20);
              }

              public boolean isFocusTraversable()
              {
                  return true;
              }

              public Dimension getPreferredSize()
              {
                  return new Dimension(50,50);
              }

              public void focusGained(FocusEvent e)
              {
                  System.out.println(e);
                  repaint();
              }

              public void focusLost(FocusEvent e)
              {
                  System.out.println(e);
                  repaint();
              }
          }
      }
      (Review ID: 128464)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            mthakore Mayank Thakore (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: