-
Enhancement
-
Resolution: Unresolved
-
P5
-
None
-
6
-
Cause Known
-
x86
-
linux
A DESCRIPTION OF THE REQUEST :
javax.swing.SpinnerNumberModel.incrValue(int) compares the new value to the minimum and maximum fields to find out if some limit has been reached. This prevents a derived class to perform more complex limit calculations by simply overriding getMinimum and getMaximum. For the Editor, this approach works well enough.
JUSTIFICATION :
Sometimes the range of a spinner control depends on the state of other controls. In such situations it is tedious to have each change of one control readjust the maximum / minimum value of the other. It would be nice to just fire change events for them and have the getMinimum and getMaximum methods of the model calculate the actual values. Direct access to the fields of SpinnerNumberModel instead of the overridable getter methods prevents this.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Comparable minimum = getMinimum();
Comparable maximum = getMaximum();
if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
return null;
}
if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
return null;
}
else {
return newValue;
}
ACTUAL -
if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
return null;
}
if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
return null;
}
---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import javax.swing.*;
class Test extends SpinnerNumberModel {
public Test() { super(0, 0, 10, 1); }
public Comparable getMinimum() {
return cb.isSelected() ? -10 : super.getMinimum();
}
private static JCheckBox cb;
public static void main(String[] args) {
cb = new JCheckBox("allow negative values");
JFrame f = new JFrame("overridden getMinimum Example");
f.getContentPane().add(new JSpinner(new Test()), BorderLayout.CENTER);
f.getContentPane().add(cb, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
It is porrible to actually adjust the minimum / maximum values each time the range changes, but it requires some more work.
It is also possible to override getNextValue() and getpreviousValue() as well to implement the range boundary checks there as well. But to restrict the range for the editor as well as for the spinner buttons, getMinimum and getMaximum still have to be overridden.
javax.swing.SpinnerNumberModel.incrValue(int) compares the new value to the minimum and maximum fields to find out if some limit has been reached. This prevents a derived class to perform more complex limit calculations by simply overriding getMinimum and getMaximum. For the Editor, this approach works well enough.
JUSTIFICATION :
Sometimes the range of a spinner control depends on the state of other controls. In such situations it is tedious to have each change of one control readjust the maximum / minimum value of the other. It would be nice to just fire change events for them and have the getMinimum and getMaximum methods of the model calculate the actual values. Direct access to the fields of SpinnerNumberModel instead of the overridable getter methods prevents this.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Comparable minimum = getMinimum();
Comparable maximum = getMaximum();
if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
return null;
}
if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
return null;
}
else {
return newValue;
}
ACTUAL -
if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
return null;
}
if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
return null;
}
---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import javax.swing.*;
class Test extends SpinnerNumberModel {
public Test() { super(0, 0, 10, 1); }
public Comparable getMinimum() {
return cb.isSelected() ? -10 : super.getMinimum();
}
private static JCheckBox cb;
public static void main(String[] args) {
cb = new JCheckBox("allow negative values");
JFrame f = new JFrame("overridden getMinimum Example");
f.getContentPane().add(new JSpinner(new Test()), BorderLayout.CENTER);
f.getContentPane().add(cb, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
It is porrible to actually adjust the minimum / maximum values each time the range changes, but it requires some more work.
It is also possible to override getNextValue() and getpreviousValue() as well to implement the range boundary checks there as well. But to restrict the range for the editor as well as for the spinner buttons, getMinimum and getMaximum still have to be overridden.