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)
======================================================================