Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to use JSpinner properly.

 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello sir, friends, madam

I am using JSpinner and following are the condition that I need to check.
1. JSpinner contains numbers with range from 1 to 720
2. Clicking the button in JSpinner should increment the value by 1
3. If the user types any value which is out of range then nearest value should be shown. Example if user doesnt enter any value in textfield of JSpinner and keeps it blank. He/She then clicks the up or down arrow button of JSpinner. Now under such cases value 1 should be displayed in the JSPinner.
4. If user types a value beyond 720 then value 720 should be displayed.

I tried using JSpinner.NumberEditor for numeric values and set the associated min, max and step values in JSpinner. The problem I am facing is when user performs 3rd and 4th steps metioned above.
I tried adding ChangeListener and DocumentListener but I am getting some kind of exceptions.

Please can anyone suggest me some right approach to come out of this problem.

Waiting for your suggestions.

Thanks and Regards
Rohit.
[ September 18, 2006: Message edited by: Rohit Bhagwat ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rohit Bhagwat:
I am getting some kind of exceptions.


99% of the time, an exception will tell you exactly what's going on. Read the message carefully, and look at the lines of code pointed to by the stack trace (the line on top is the one that where the error occurred.) If it doesn't make sense to you, cut-and-paste it here for us to see.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
set your own UI, and override createNextButton() and createPreviousButton()
remove the actionListeners from the above buttons
add your own actionListener where you check the textField's text (not the spinner's value),
and adjust if outside of range.
you will also need code to + or - for normal inside-range values
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the stack trace.
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1343)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:662)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1441)
at javax.swing.JFormattedTextField$AbstractFormatter.install(JFormattedTextField.java:925)
at javax.swing.text.DefaultFormatter.install(DefaultFormatter.java:105)
at javax.swing.text.InternationalFormatter.install(InternationalFormatter.java:268)
at javax.swing.JFormattedTextField.setFormatter(JFormattedTextField.java:443)
at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:767)
at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:480)
at javax.swing.JSpinner$DefaultEditor.stateChanged(JSpinner.java:696)
at javax.swing.JSpinner.fireStateChanged(JSpinner.java:426)
at javax.swing.JSpinner$ModelListener.stateChanged(JSpinner.java:353)
at javax.swing.AbstractSpinnerModel.fireStateChanged(AbstractSpinnerModel.java:102)
at javax.swing.SpinnerNumberModel.setValue(SpinnerNumberModel.java:430)
at com.core.java_ui.pagesetup.OptionsTab$SpinnerDocumentListener.updateSpinner(OptionsTab.java:294)
at com.core.java_ui.pagesetup.OptionsTab$SpinnerDocumentListener.insertUpdate(OptionsTab.java:258)
at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:184)
at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:754)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:711)
at javax.swing.text.PlainDocument.insertString(PlainDocument.java:114)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:673)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1441)
at javax.swing.JFormattedTextField$AbstractFormatter.install(JFormattedTextField.java:925)
at javax.swing.text.DefaultFormatter.install(DefaultFormatter.java:105)
at javax.swing.text.InternationalFormatter.install(InternationalFormatter.java:268)
at javax.swing.JFormattedTextField.setFormatter(JFormattedTextField.java:443)
at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:767)
at javax.swing.JFormattedTextField.processFocusEvent(JFormattedTextField.java:614)
at java.awt.Component.processEvent(Component.java:5244)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1810)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:827)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:478)
at java.awt.Component.dispatchEventImpl(Component.java:3841)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Craig. Actually I feel this is slightly less user friendly because user cant clear up the value in the spinner and type in some fresh value. The constraint is that some valid value must be present. Actually in my case user can clear up any value and can also type in any invalid value. But once he presses the increment or decrement arrow button associated with JSpinner then the nearest limiting value should be displayed. i.e if he clears all the value in the JSpinner then on clicking the increment or decrement buttons should result in displaying of minimum value in JSpinner. i.e if the lower limit is 1 and if the user clears the value from JSpinner then clicking the buttons should result in displaying value 1 in JSpinner. On similar lines if the user types large value which is greater than maximum value of JSpinner then on clicking the increment and decrement buttons should result in displaying of maximum value of JSpinner. Example if max value of JSpinner is 720 and user types say 72000 and clicks the increment/decrement buttons then 720 value should be displayed in the JSpinner.

Waiting for your suggestions.

Thanks and Regards
Rohit.
[ September 18, 2006: Message edited by: Rohit Bhagwat ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic