JDK-4760088 : JSpinner(or it's editor) does not fire KeyEvent
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0,1.4.2,6
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic,windows_2000,windows_xp
  • CPU: generic,x86
  • Submitted: 2002-10-08
  • Updated: 2008-07-17
  • Resolved: 2008-07-17
Related Reports
Duplicate :  
Description
Name: sv35042			Date: 10/08/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

FULL OPERATING SYSTEM VERSION : Windows 2000 5.00.2195 SP2


ADDITIONAL OPERATING SYSTEMS : n/a



A DESCRIPTION OF THE PROBLEM :
The JSpinner class supports the addKeyListener method of
Component, but the spinner never calls the keyPressed,
keyReleased, or keyTyped methods of an object implementing
the KeyListener interface.  Also, using getEditor
().addKeyListener, to add a KeyListener to the JComponent
that is the JSpinner editor works without error, however,
it never calls any of the KeyListener methods either.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a JSpinner with a model.
2. addKeyListener directly to the JSpinner object
3. addKeyListener to the JSpinner editor retrieved by
getEditor.
4. Implement keyPressed, keyReleased, keyTyped, and verify
that these methods are never invoked when a keyboard action
occurs on the spinner.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Each of the KeyListener methods should have been invoked
due to pressing a key while the JSpinner had focus.  This
never happened.

This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*
import javax.swing.*;

public class Test extends JFrame implements KeyListener {

  public static void main(String args[]) {
    new Test();
  }

  public Test() {
    SpinnerNumberModel model = new SpinnerNumberModel();
    model.setMinimum(new Integer(1));
    model.setMaximum(new Integer(10));

    JSpinner spinner = new JSpinner(model);
    spinner.addKeyListener(this);
    spinner.getEditor().addKeyListener(this);

    getContentPane().add(spinner);
    setSize(100, 100);
    show();
  }

  public void keyPressed(KeyEvent ke) {
    System.out.println("key " + ke.getKeyCode() + " pressed on " + ke.getSource
());
  }

  public void keyReleased(KeyEvent ke) {
    System.out.println("key " + ke.getKeyCode() + " released on " + ke.getSource
());
  }

  public void keyTyped(KeyEvent ke) {
    System.out.println("key " + ke.getKeyCode() + " typed on " + ke.getSource
());
  }

---------- END SOURCE ----------
(Review ID: 143609) 
======================================================================

Comments
EVALUATION It doesn't look correct to notify one component about keyEvents from the other one, moreover there is a clear workaround how to add a listener to the actual textField: ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addKeyListener(this); note that in this case it is much better to add a DocumentListener to the textField's document instead of KeyListener closed as will not fix
17-07-2008

EVALUATION Contribution-forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=11066
27-01-2006