JDK-4687245 : On TAB, JSpinner first turns focus, then calls ChangeListener
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2002-05-17
  • Updated: 2008-05-14
Related Reports
Relates :  
Description
Name: jk109818			Date: 05/17/2002


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

FULL OPERATING SYSTEM VERSION :
Microsoft Windows XP [Version 5.1.2600]

ADDITIONAL OPERATING SYSTEMS :
Microsoft Windows 2000

A DESCRIPTION OF THE PROBLEM :
If you edit(!) a JSpinner and press TAB afterwards, first
the focus turns and after that, any ChangeListeners on the
JSpinner are called.

This leads to the following problem: If you put a
ChangeListener on a JSpinner for controlling values of the
JSpinner, and want to disable the next (or last) Component
in some cases for some values of the JSpinner, the next
(or last) Component will be disables AND get Focus (that's
the bug). Now the Focus is "catched" on the disabled
Component :-(((

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the given code.
2. Edit (!, do not spin) the value to 5.
3. Press TAB

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected Result: JButton gets disabled and Focus stays on
JSpinner.

Actual Result: JButton gets disabled (ok) AND gets Focus.

This bug can be reproduced always.

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

public class SpinnerBug extends JFrame {
  private JButton jButton = new JButton();
  private JSpinner jSpinner = new JSpinner();

  public SpinnerBug() {
    getContentPane().setLayout(new FlowLayout());
    jButton.setText("jButton");
    jSpinner.setModel(new SpinnerNumberModel(1,1,10,1));
    jSpinner.getModel().addChangeListener(new ChangeListener(){
      public void stateChanged(ChangeEvent e){
        if (((Integer)jSpinner.getValue()).intValue() == 5){
          jButton.setEnabled(false);
        } else {
          jButton.setEnabled(true);
        }
      }
    });
    getContentPane().add(jButton);
    getContentPane().add(jSpinner);
    pack();
    setVisible(true);
  }

  public static void main(String[] args) {
    SpinnerBug spinnerBug = new SpinnerBug();
  }
}

---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Try to listen to the TextField of the Editor and disable
Component "on the fly".
(Review ID: 146728) 
======================================================================

Comments
EVALUATION we shouldn't make focus synchronouse, because this will create a lot of problems for old code. But we should looks if we can fix problem with disabled components and focus
14-05-2008

EVALUATION This happens because of the order of events. I'm reassigning to AWT for two reasons: . disabling a component should make it so that it doesn't get focus. . focus should be sychronous:) ###@###.### 2003-08-11
11-08-2003