-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
1.4.2
-
x86
-
windows_xp
A DESCRIPTION OF THE REQUEST :
when JSpinner class has a focus with the default editor model ( for example, NumberEditor ), it is impossible to get the value. It is necessary to call commitEdit() before getValue() will return the correct result.
If the JSpinner class has minimum and maximum values, and the user entered the incorrect value, the commitEdit() method throws the exception, and the value entered by the user is lost.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class JSpinnerTest {
JSpinner spinner;
JDialog jDialog;
JButton ok;
void initGUI() {
jDialog = new JDialog();
jDialog.setModal(true);
jDialog.getContentPane().setLayout( new GridLayout(2,1) );
spinner = new JSpinner( new SpinnerNumberModel(1, 1, 100, 1) );
ok = new JButton("ok");
ok.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
System.out.println(spinner.getValue().toString()
+ "\n");
((JSpinner.NumberEditor)spinner.getEditor()).commitEdit();
System.out.println(spinner.getValue().toString()
+ "\n");
} catch (Exception ex) {
System.out.println("The incorrect value
entered by user is : " +
spinner.getValue().toString() + "\n");
ex.printStackTrace();
}
jDialog.dispose();
};
} );
jDialog.getContentPane().add( spinner );
jDialog.getContentPane().add( ok );
jDialog.show();
}
public static void main(String[] args) {
JSpinnerTest tst = new JSpinnerTest();
tst.initGUI();
};
};
JUSTIFICATION :
It is impossible to correct the value entered by the user, as you do not have the possibility to extract the exact value from the editor control. To reproduce the problem, enter 1111 into the spinner editor and click on the "Ok" button. GetValue() will return 1 even after the commitEdit() is called.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
create a method which will allow to retreive the value from the currently edited JSpinner
CUSTOMER SUBMITTED WORKAROUND :
1. Creating the own editor model.
2. Notifying the user about the "incorrect value".
when JSpinner class has a focus with the default editor model ( for example, NumberEditor ), it is impossible to get the value. It is necessary to call commitEdit() before getValue() will return the correct result.
If the JSpinner class has minimum and maximum values, and the user entered the incorrect value, the commitEdit() method throws the exception, and the value entered by the user is lost.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class JSpinnerTest {
JSpinner spinner;
JDialog jDialog;
JButton ok;
void initGUI() {
jDialog = new JDialog();
jDialog.setModal(true);
jDialog.getContentPane().setLayout( new GridLayout(2,1) );
spinner = new JSpinner( new SpinnerNumberModel(1, 1, 100, 1) );
ok = new JButton("ok");
ok.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
System.out.println(spinner.getValue().toString()
+ "\n");
((JSpinner.NumberEditor)spinner.getEditor()).commitEdit();
System.out.println(spinner.getValue().toString()
+ "\n");
} catch (Exception ex) {
System.out.println("The incorrect value
entered by user is : " +
spinner.getValue().toString() + "\n");
ex.printStackTrace();
}
jDialog.dispose();
};
} );
jDialog.getContentPane().add( spinner );
jDialog.getContentPane().add( ok );
jDialog.show();
}
public static void main(String[] args) {
JSpinnerTest tst = new JSpinnerTest();
tst.initGUI();
};
};
JUSTIFICATION :
It is impossible to correct the value entered by the user, as you do not have the possibility to extract the exact value from the editor control. To reproduce the problem, enter 1111 into the spinner editor and click on the "Ok" button. GetValue() will return 1 even after the commitEdit() is called.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
create a method which will allow to retreive the value from the currently edited JSpinner
CUSTOMER SUBMITTED WORKAROUND :
1. Creating the own editor model.
2. Notifying the user about the "incorrect value".