Reputation: 3
I currently have a JSpinner
implemented into my form which holds the amount of time taken to do a task; the current limit of this spinner is 180 minutes. My question is how would I detect that the spinner is currently at it's max value so that when the user tries to press the button to increment the amount of minutes I can output a message that notifies them that the max amount of time to perform a task is 180 minutes?
Upvotes: 0
Views: 284
Reputation: 1
I can give a workaround, you just set the limit to 181, and check the value in the changelistener, if it equals to 181, set the value to 180, and perform your method.
Upvotes: 0
Reputation: 1
You can add a change listener to your JSpinner component. In its stateChanged
method you can call the getNextValue()
method. If this returns null
then you have reached the max value of the model.
See more: https://docs.oracle.com/javase/8/docs/api/javax/swing/JSpinner.html#getNextValue--
Upvotes: 0
Reputation: 1160
Once a JSpinner has reached the limit value, you don't get any more change events from it.
Create a subclass of javax.swing.SpinnerNumberModel that has a custom "setValue(..)" method that notifies your code when the UI attempts to set a value that's too large. Then install it in your JSpinner object with
mySpinner.setModel(new MySpinnerModel())
But a larger question is whether you should be doing this at all. A JSpinner has a well-understood UI; people are (meant to be) used to how it performs. Getting a dialog box that you'll have to dismiss or some other unexpected behavior might not make them happy.
Upvotes: 1