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

Possible Conflict between KeyListener and ActionListener?

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.1.4
    • client-libs
    • x86
    • windows_95



      Name: diC59631 Date: 12/08/97


      1. Steps To reproduce it
      =========================
      i. Compile attached codes, run it using an
         appletviewer
      ii.Use the key "c" to clear, and then the button
         "clear". After hitting the clear button, the
         key listener will stop working.

      2. Souce Code
      ==============
      //WhiteBoard2.java

      import java.applet.*;
      import java.awt.*;
      import java.awt.event.*;

      public class WhiteBoard3 extends Applet
      {
      private int lastx, lasty;

      public void init()
      {
      //Anonymous class for mouse Event (action type)
      this.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e)
      {
      lastx=e.getX();
      lasty = e.getY();
      }
      });

      //Anonymous class for mouse Event (motion type)
      this.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e)
      {
      Graphics g = getGraphics();
      int x = e.getX(), y = e.getY();
      g.drawLine (lastx, lasty, x, y);
      lastx=x;
      lasty=y;
      }
      });

      //Anonymous class for keyboard Event (key type)
      this.requestFocus();

      this.addKeyListener(new KeyAdapter () {
      public void keyTyped(KeyEvent e)
      {
      //System.out.println("Key Adapter Processed");
      if (e.getKeyChar() == 'c')
      {
      Graphics g = getGraphics();
      g.setColor(getBackground());
      g.fillRect(0,0, getSize().width, getSize().height);
      }
      }
      });


      //'Clear' Button
      //1. Create
      Button cb = new Button("Clear");

      //2. Add a Action Listener
      cb.addActionListener (new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
      System.out.println("Action Listener Processed");
      Graphics g = getGraphics();
      g.setColor(getBackground());
      g.fillRect(0,0, getSize().width, getSize().height);
      }
      });

      //3. Add to applet
      this.add(cb);
      }
      }


      3. Exact Error Message
      ======================
      None

      4. - none

      5. Additonal Info
      =================
      The error seems to be the case for any keys used.
      Tried with 3 keys; s, c, a
      (Review ID: 21512)
      ======================================================================

            rramsunw Ranganathan Ram (Inactive)
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: