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

lightweight components mouse tracking not delivering events, causing exception

    XMLWordPrintable

Details

    • b01
    • x86
    • windows_nt
    • Not verified

    Description

      /*****************************************************************
      This program will illustrate a bug with mouse tracking when
      lightweight containers and components that overlap each other
      are not necessarily direct descendants or siblings of each other.
      Two frames will be crated: the one with a lightweight component
      structure and another with a heavyweight replica. Watch the
      differences in responces to mouse movements in the frames:
      1) The Magenta component never gets any mouse move events
      2) The Yellow component only gets mouse events after the
      mouse crosses the Red/Green boundary
      3) If you move the mouse underneath the Yellow component, the
      Yellow component will receive mouse move event instead of the
      Red or Green when the pointer crosses the Red/Green boundary

      4) If you hit Ctrl+S when a frame is active, the main panel
      for that frame will be saved (HEAVY.PNL for the heavyweight
      and LIGHT.PNL for the lightweight). If you then hit Ctrl+O
      and pick the corresponding file from the file dialog by
      double-clicking on it and keep the mouse moving inside the
      frame, you'll be getting continuous exceptions in the
      LightWeightDispatcher

      The 1, 2 and 4 happen in JDK 1.1 & JDK 1.1.1, while the 3 is
      JDK 1.1 only
      3/10/97 11:33AM
      *****************************************************************/

      import java.awt.*;
      import java.io.*;
      import java.awt.event.*;

      /*****************************************************************
      A lightweight container
      3/10/97 11:37AM
      *****************************************************************/
      class LightPanel extends Container {
      public LightPanel() {
      super();
      }
      } // finish class LightPanel

      /*****************************************************************
      A heavyweight container
      3/10/97 11:37AM
      *****************************************************************/
      class HeavyPanel extends Panel {
      public HeavyPanel() {
      super();
      }
      } // finish class HeavyPanel


      /*****************************************************************
      A lightweight component
      3/10/97 11:38AM
      *****************************************************************/
      class LightComponent extends Component {
      private String name;

      public LightComponent(String str) {
      super();

      name = str;
      }

      public void update(Graphics g) {
      paint(g);
      }

      public void paint(Graphics g) {
      g.setColor(getBackground());
      g.fillRect(0, 0, getSize().width, getSize().height);
      }

          public String toString() {
      return name;
      }
      } // finish class LightComponent


      /*****************************************************************
      A heavyweight component
      3/10/97 11:39AM
      *****************************************************************/
      class HeavyComponent extends Canvas {
      private String name;

      public HeavyComponent(String str) {
      super();

      name = str;
      }

      public void update(Graphics g) {
      paint(g);
      }

      public void paint(Graphics g) {
      g.setColor(getBackground());
      g.fillRect(0, 0, getSize().width, getSize().height);
      }

          public String toString() {
      return name;
      }
      } // finish class HeavyComponent

      /*****************************************************************
      Responds to mouse movement events in the components
      3/10/97 11:42AM
      *****************************************************************/
      class MouseHandler extends MouseMotionAdapter {
      TextField status;

      public MouseHandler(TextField text) {
      status = text;
      }

          public void mouseMoved(MouseEvent e) {
      Component source = e.getComponent();
      if ( source != null ) {
      String newText = "Mouse has moved in the " + source.toString() + " component";
      String oldText = status.getText();
      if ( !newText.equals(oldText) ) {
      status.setText(newText);
      }
      }
      }
      } // finish class MouseHandler



      /*****************************************************************
      Handles the frame close events
      3/10/97 1:28PM
      *****************************************************************/
      class FrameHandler extends WindowAdapter {
           public void windowClosing(WindowEvent e) {
      System.exit(0);
      }

           public void windowActivated(WindowEvent e) {
      ((Frame)e.getSource()).requestFocus();
      }
      } // finish class FrameHandler

      /*****************************************************************
      Contains the lightweight tree
      3/10/97 12:09PM
      *****************************************************************/
      class LightFrame extends Frame {
      protected LightPanel main;
      private TextField text;

      /*****************************************************************
      Handles the keystrokes for the frame
      3/10/97 3:05PM
      *****************************************************************/
      class LightKeyHandler extends KeyAdapter {
      public void keyPressed(KeyEvent e) {
      // Ctrl+S == 19
      // Ctrl+O == 15

      if ( e.getKeyChar() == 19 ) {
      try {
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("LIGHT.PNL"));
      oos.writeObject(main);
      oos.close();
      }

      catch ( Exception oe ) {
      text.setText(oe.toString());
      }
      }
      else if ( e.getKeyChar() == 15 ) {
      FileDialog fd = new FileDialog(LightFrame.this, "Open file");
      fd.show();
      String fname = fd.getFile();
      if ( fname != null ) {
      try {
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fname));

      LightPanel panel = (LightPanel)ois.readObject();
      ois.close();

      if ( panel != null ) {
      remove(main);
      main = panel;
      add(main);
      validate();
      repaint();
      }
      }

      catch ( Exception ie ) {
      text.setText(ie.toString());
      }
      }
      }
      }
      } // finish class LightKeyHandler

      public LightFrame(String title) {
      super(title);

      setLayout(null);

      text = new TextField();
      text.setEditable(false);
      MouseHandler lightHandler = new MouseHandler(text);

      LightComponent comp = new LightComponent("Yellow");
      comp.setBackground(Color.yellow);
      add(comp);
      comp.setBounds(100, 50, 100, 100);
      comp.addMouseMotionListener(lightHandler);

      comp = new LightComponent("Magenta");
      comp.setBackground(Color.magenta);
      add(comp);
      comp.setBounds(20, 50, 40, 50);
      comp.addMouseMotionListener(lightHandler);

      main = new LightPanel();
      main.setLayout(new GridLayout(1, 2));

      comp = new LightComponent("Red");
      comp.setBackground(Color.red);
      main.add(comp);
      comp.addMouseMotionListener(lightHandler);

      comp = new LightComponent("Green");
      comp.setBackground(Color.green);
      main.add(comp);
      comp.addMouseMotionListener(lightHandler);

      add(main);
      add(text);

      addKeyListener(new LightKeyHandler());
      }

      public void doLayout() {
      main.setBounds(0, 0, getSize().width, getSize().height - 30);
      main.doLayout();

      text.setBounds(0, getSize().height - 30, getSize().width, 30);
      }
      } // finish class LightFrame

      /*****************************************************************
      Contains the heavyweight tree
      3/10/97 1:11PM
      *****************************************************************/
      class HeavyFrame extends Frame {
      private HeavyPanel main;
      private TextField text;

      /*****************************************************************
      Handles the keystrokes for the frame
      3/10/97 3:07PM
      *****************************************************************/
      class HeavyKeyHandler extends KeyAdapter {
      public void keyPressed(KeyEvent e) {
      // Ctrl+S == 19
      // Ctrl+O == 15

      if ( e.getKeyChar() == 19 ) {
      try {
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("HEAVY.PNL"));
      oos.writeObject(main);
      oos.close();
      }

      catch ( Exception oe ) {
      text.setText(oe.toString());
      }
      }
      else if ( e.getKeyChar() == 15 ) {
      FileDialog fd = new FileDialog(HeavyFrame.this, "Open file");
      fd.show();
      String fname = fd.getFile();
      if ( fname != null ) {
      try {
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fname));

      HeavyPanel panel = (HeavyPanel)ois.readObject();
      ois.close();

      if ( panel != null ) {
      remove(main);
      main = panel;
      add(main);
      validate();
      repaint();
      }
      }

      catch ( Exception ie ) {
      text.setText(ie.toString());
      }
      }
      }
      }
      } // finish class HeavyKeyHandler

      public HeavyFrame(String title) {
      super(title);

      setLayout(null);

      text = new TextField();
      text.setEditable(false);
      MouseHandler heavyHandler = new MouseHandler(text);

      HeavyComponent comp = new HeavyComponent("Yellow");
      comp.setBackground(Color.yellow);
      add(comp);
      comp.setBounds(100, 50, 100, 100);
      comp.addMouseMotionListener(heavyHandler);

      comp = new HeavyComponent("Magenta");
      comp.setBackground(Color.magenta);
      add(comp);
      comp.setBounds(20, 50, 40, 50);
      comp.addMouseMotionListener(heavyHandler);

      main = new HeavyPanel();
      main.setLayout(new GridLayout(1, 2));

      comp = new HeavyComponent("Red");
      comp.setBackground(Color.red);
      main.add(comp);
      comp.addMouseMotionListener(heavyHandler);

      comp = new HeavyComponent("Green");
      comp.setBackground(Color.green);
      main.add(comp);
      comp.addMouseMotionListener(heavyHandler);

      add(main);
      add(text);

      addKeyListener(new HeavyKeyHandler());
      }

      public void doLayout() {
      main.setBounds(0, 0, getSize().width, getSize().height - 30);
      main.doLayout();

      text.setBounds(0, getSize().height - 30, getSize().width, 30);
      }
      } // finish class HeavyFrame

      public class LightWeightMouseMoveBug {
      public static void main(String [] args) {
      FrameHandler frameHandler = new FrameHandler();
      LightFrame lightFrame = new LightFrame("Light weight");
      HeavyFrame heavyFrame = new HeavyFrame("Heavy weight");

      lightFrame.setResizable(false);
      heavyFrame.setResizable(false);

      lightFrame.addWindowListener(frameHandler);
      heavyFrame.addWindowListener(frameHandler);

      lightFrame.pack();
      lightFrame.setBounds(0, 0, 300, 200);
      lightFrame.setVisible(true);

      heavyFrame.pack();
      heavyFrame.setBounds(320, 0, 300, 200);
      heavyFrame.setVisible(true);
      }
      } // finish class LightWeightMouseMoveBug


      The bugs do occur in Win95, NT, and Solaris. Unfortunately, the steps we indicate to reproduce the exception in the lightweight dispatcher do not actually reproduce the problem on Solaris. We believe there might be another bug with the keyboard event handling (under investigation) that gets in the way.
      Instead, if you invoke the dialog
      via a mouse click (not in our supplied sample code) and then follow the steps to reproduce,
      you will get the exception.

      Attachments

        Issue Links

          Activity

            People

              tprinzing Tim Prinzing
              rschiavisunw Richard Schiavi (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: