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

Incorrect MouseEvent handling when mixing light and heavyweight components

XMLWordPrintable

    • x86
    • windows_95



      Name: tb29552 Date: 10/08/98


      /*

      Hi, In the course of trying to find a way to correctly
          shift focus around components using the tab and
          shift-tab combinations I found the following
          problem which I have not seen reported. It occurs
          when mixing heavy and lightweight textfield
          components (TextField and JTextField) and involves
          Events being handled by incorrect components.

          Running the sample code I have provided below will
          create a small frame containing a JTextfield,
          TextField, JTextField, and JButton vertically and
          in that order. The sample code provides
          processMouseEvent, processKeyEvent and
          processFocusEvent methods. By moving the events
          between components and clicking with the mouse on
          these components it is easy to see that for some
          reason the mouse events for the TextField (text2
          variable) are being detected and handled by the
          first JTextField component (text1 variable). This
          is obviously incorrect behaviour.

      */
      //import com.sun.java.swing.*;
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      class CFTest extends JFrame {
          JTextField text1;
          TextField text2;
          JTextField text3;
          JButton button1;

          CFTest ()
          {
              // Disable swing focus manager as suggested when mixing
              // lightweight and heavyweight components. Focus Manager
              // should now work as per JDK1.1 AWT Focus Manager.
              DefaultFocusManager.disableSwingFocusManager();

              // Initialise text fields and buttons, overriding
              // processMouse, Keym and Focus events. Consume tab
              // key events to prevent focus shift between components
              text1 = new JTextField() {
                  public void processKeyEvent(KeyEvent e)
                  {
                      System.out.println("Text 1: Key Event");
                      if (e.getKeyCode() == KeyEvent.VK_TAB)
                          e.consume();
                      else
                          super.processKeyEvent(e);
                  }

                  public void processFocusEvent(FocusEvent e)
                  {
                      System.out.println("Text 1: Focus Event");
                      super.processFocusEvent(e);
                  }

                  public void processMouseEvent(MouseEvent e)
                  {
                      System.out.println("Text 1: Mouse Event");
                      super.processMouseEvent(e);
                  }
              };
              text2 = new TextField() {
                  public void processKeyEvent(KeyEvent e)
                  {
                      System.out.println("Text 2: Key Event");
                      if (e.getKeyCode() == KeyEvent.VK_TAB)
                          e.consume();
                      else
                          super.processKeyEvent(e);
                  }

                  public void processFocusEvent(FocusEvent e)
                  {
                      System.out.println("Text 2: Focus Event");
                      super.processFocusEvent(e);
                  }

                  public void processMouseEvent(MouseEvent e)
                  {
                      System.out.println("Text 2: Mouse Event");
                      super.processMouseEvent(e);
                  }
              };
              button1 = new JButton() {
                  public void processKeyEvent(KeyEvent e)
                  {
                      System.out.println("Button 1: Key Events");
                      if (e.getKeyCode() == KeyEvent.VK_TAB)
                          e.consume();
                      else
                          super.processKeyEvent(e);
                  }

                  public void processFocusEvent(FocusEvent e)
                  {
                      System.out.println("Button 1: Focus Event");
                      super.processFocusEvent(e);
                  }

                  public void processMouseEvent(MouseEvent e)
                  {
                      System.out.println("Button 1: Mouse Event");
                      super.processMouseEvent(e);
                  }
              };
              text3 = new JTextField() {
                  public void processKeyEvent(KeyEvent e)
                  {
                      System.out.println("Text 3: Key Events");
                      if (e.getKeyCode() == KeyEvent.VK_TAB)
                          e.consume();
                      else
                          super.processKeyEvent(e);
                  }

                  public void processFocusEvent(FocusEvent e)
                  {
                      System.out.println("Text 3: Focus Event");
                      super.processFocusEvent(e);
                  }

                  public void processMouseEvent(MouseEvent e)
                  {
                      System.out.println("Text 3: Mouse Event");
                      super.processMouseEvent(e);
                  }
              };

              // add components to frame
              this.getContentPane().setLayout(new GridLayout(0, 1));
              this.getContentPane().add(text1);
              this.getContentPane().add(text2);
              this.getContentPane().add(text3);
              this.getContentPane().add(button1);

              this.pack();

              // local listener class for detecting exit
              this.addWindowListener(new WindowAdapter()
                                     {
                  public void windowClosing(WindowEvent e)
                  {
                      dispose();
                      System.exit(1);
                  }
              });

              // display frame
              this.show();
          }

          // main method to create and display CFTest frame
          public static void main(String args[])
          {
              CFTest t = new CFTest ();
          }
      }

      (Review ID: 38069)
      ======================================================================

            mbronsonsunw Mike Bronson (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: