JDK-4906034 : make requesting focus synchronous
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.0,6
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 2003-08-13
  • Updated: 2007-01-31
Related Reports
Duplicate :  
Description
Name: jk109818			Date: 08/13/2003


A DESCRIPTION OF THE REQUEST :
I have 3 components in my sample application JSPinner, JButton and JLabel. I am typing a value in the JSpinner press on the button and want the label to display the current spinner value. Sometimes after a button press the label displays the old value. That means the spinner fails to commit by the time I need to use the value.

JUSTIFICATION :
The current behavior makes JSpinner very unreliable.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Since JFormattedTextField's default behavior is to commit on focus lost pressing on the button should always cause the JSpinner to commit since the focus is transferred to the button.
ACTUAL -
Sometimes the JSpinner doesn't commit in time.

---------- BEGIN SOURCE ----------
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SwingUtilities;

/**
 * @author allak
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class TestSpinner extends JFrame{
	
	JLabel display;
	JSpinner spinner;
	
	public void init(){
		spinner = new JSpinner();
		JButton button = new JButton("Press");
		display = new JLabel();
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				display.setText(spinner.getValue().toString());
			}
		});
		System.out.println("adding stuff");
		this.getContentPane().setLayout(new FlowLayout());
		this.getContentPane().add(new JSpinner());
		this.getContentPane().add(spinner);
		this.getContentPane().add(button);
		this.getContentPane().add(display);
	}
	public void setFocus(){
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().requestFocusInWindow();
			}
		});
		
	}
	public static void main(String [] args){
		TestSpinner testSpinner = new TestSpinner();
		testSpinner.init();
		
		testSpinner.setSize(300, 300);
		testSpinner.show();
		try{
		Thread.sleep(1000);
		}catch(Exception e){
		
		}
		testSpinner.setFocus();
	}
	
	

}

---------- END SOURCE ----------
(Incident Review ID: 186407) 
======================================================================

Comments
EVALUATION 4947031 (SequencedEvent should be eliminated) should be fixed before implementing this rfe
31-01-2007

EVALUATION This is happening because focus delivery is not synchronous. ###@###.### 2003-08-13 Name: osR10079 Date: 08/20/2003 Too late for Tiger. We should consider implementation of synchronous focus requests in next release. But we should keep in mind that they should be eihter syncronous or asynchronous for both lightweight and heavyweight components. ###@###.### 20 Aug 2003 ======================================================================
14-08-2004