Re: JSpinner question
- From: Roland de Ruiter <roland.de.ruiter@xxxxxxxxxxxxxxx>
- Date: Mon, 17 Jul 2006 22:57:02 +0200
On 17-7-2006 19:08, Alex wrote:
I want my JSpinner control to change step based on current value ofCreate your own implementation of the SpinnerModel interface and set this as the model of your JSpinner. For example:
spinner
Like this: if spinner value is less that 10 step is 1, if spinner value
becomes more than 10 step will be 3
Thanks,
Alex
import java.awt.BorderLayout;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SwingUtilities;
public class MySpinnerModel extends AbstractSpinnerModel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MySpinnerModel mySpinnerModel = new MySpinnerModel();
JSpinner spinner = new JSpinner();
spinner.setModel(mySpinnerModel);
JFrame app = new JFrame();
app.setSize(400, 300);
app.getContentPane().add(spinner, BorderLayout.NORTH);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.validate();
app.setVisible(true);
}
});
}
private int lowerBound = -20;
private int upperBound = +20;
private int value = 0;
public Object getNextValue() {
int newValue;
if (this.value <= 10) {
newValue = this.value + 1;
} else {
newValue = this.value + 3;
}
if (newValue <= upperBound) {
return new Integer(newValue);
} else {
return null;
}
}
public Object getPreviousValue() {
int newValue;
if (this.value <= 10) {
newValue = this.value - 1;
} else {
newValue = this.value - 3;
}
if (newValue >= lowerBound) {
return new Integer(newValue);
} else {
return null;
}
}
public Object getValue() {
return new Integer(value);
}
private void setValue(int value) {
if (this.value != value) {
this.value = value;
fireStateChanged();
}
}
public void setValue(Object value) {
if (value instanceof String) {
try {
setValue(Integer.parseInt(((String) value).trim()));
} catch (NumberFormatException ex) {
System.err.println("Unsupported value: " + value);
}
} else if (value instanceof Number) {
setValue(((Number) value).intValue());
} else {
System.err.println("Unsupported value: " + value);
}
}
}
--
Regards,
Roland
.
- References:
- JSpinner question
- From: Alex
- JSpinner question
- Prev by Date: JSpinner question
- Next by Date: helpful advice
- Previous by thread: JSpinner question
- Next by thread: helpful advice
- Index(es):