Weird JSpinner behavior
I'm currently using a JSpinner in my application with a SpinnerNumberModel. I gave this model a maximum value of 9999.
Thinking this would be enough for the JSpinner to function correctly, it still gives problems.
When I go past the 9999 value, it returns to 1.000. I never asked for these decimals to appear, nor for it to loop. Is this normal behavior? How can I prevent this?
If the user types in something like 99999999, and then uses the arrows next to it, the number changes to a decimal number. This is also completely unwanted, I want it to automatically go back to 9999.
Also, how come I can type in letters instead of numbers only? You'd think a JSpinner is numbers-only....
[719 byte] By [
N00dlesa] at [2007-11-27 11:30:42]

# 1
> When I go past the 9999 value, it returns to 1.000. I never asked for these
> decimals to appear, nor for it to loop.
> Is this normal behavior?
No
> How can I prevent this?
you won't, unless you post your code.
Simple example, just like this
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
class Testing
{
public void buildGUI()
{
JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 1000, 1));
//((DefaultFormatter)((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().getFormatter()).setAllowsInvalid(false);
JFrame f = new JFrame();
f.getContentPane().add(spinner,BorderLayout.NORTH);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
> Also, how come I can type in letters instead of numbers only? You'd think a JSpinner is numbers-only....
uncomment line in above demo
# 2
Ok, thanks for the reply. The letters-problem is solved now. Here's a compilable program that shows the weird decimal-behavior. The maximum valie is 9999. However, past 999 it changes to 1.000...Likewise, if you type in more than 3 numbers, the decimals will appear.
Hope you can help me on this. :)
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
public class SpinnerTest extends JFrame {
private JFrame windowAddScn;
private JPanel jPanel = new JPanel();
private JSpinner spinnerScnNum;
private SpinnerNumberModel spinnerModel;
private BorderLayout layout = new BorderLayout();
public SpinnerTest() {
setTitle("Spinner test");
setResizable(false);
spinnerScnNum = new JSpinner();
spinnerModel = new SpinnerNumberModel(0,0,9999,1);
spinnerScnNum.setModel(spinnerModel);
((DefaultFormatter)((JSpinner.DefaultEditor)spinnerScnNum.getEditor()).getTextField().getFormatter()).setAllowsInvalid(false);
this.getContentPane().add(spinnerScnNum,BorderLayout.NORTH);
setLocationRelativeTo(null);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new SpinnerTest();
}
}