java - JSpinner with Display Format, Numbers only and manual edit -


it required create jspinner following features.

  • range should 0 1000 10 increment.
  • display format should 4 digits. (ex : 10 -> 0010)
  • it must allow user enter values manually.
  • it should not allow characters other digits.
  • it should not allow enter values having more 3 digits.

this current implementation :

spinnerupdown.setmodel(new spinnernumbermodel(0,0,1000,10)); spinnerupdown.seteditor(new jspinner.numbereditor(spinnerupdown,"0000")); jformattedtextfield txt = ((jspinner.numbereditor) spinnerupdown.geteditor()).gettextfield(); ((numberformatter) txt.getformatter()).setallowsinvalid(false); 

problem implementation is, not allow user manually enter value.

if remove line :

((numberformatter) txt.getformatter()).setallowsinvalid(false); 

it allow me enter values manually, allows enter letters also.

any suggestions on come problem. thank you!!

jspinner typically uses jtextcomponent editor. editor , apply documentfilter it.

this allow filter text coming document.

check these, cited, examples

example

jformattedtextfield installing it's own documentfilter. can overcome providing own plaindocument returns it's own filter

import java.awt.eventqueue; import java.awt.gridbaglayout; import javax.swing.jframe; import javax.swing.jspinner; import javax.swing.spinnernumbermodel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.text.abstractdocument; import javax.swing.text.attributeset; import javax.swing.text.badlocationexception; import javax.swing.text.documentfilter; import javax.swing.text.jtextcomponent; import javax.swing.text.plaindocument;  public class testspinner01 {      public static void main(string[] args) {         new testspinner01();     }      public testspinner01() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  jspinner spinnerupdown = new jspinner();                 spinnerupdown.setmodel(new spinnernumbermodel(0, 0, 1000, 10));                 spinnerupdown.seteditor(new jspinner.numbereditor(spinnerupdown, "0000"));                 system.out.println(spinnerupdown.geteditor());                   jframe frame = new jframe("test");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.setlayout(new gridbaglayout());                 frame.add(spinnerupdown);                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);                  plaindocument doc = new plaindocument() {                     private testfilter filter;                     @override                     public documentfilter getdocumentfilter() {                         if (filter == null) {                             filter = new testfilter();                         }                         return filter;                     }                  };                  jtextcomponent txt = ((jspinner.defaulteditor) spinnerupdown.geteditor()).gettextfield();                 txt.setdocument(doc);             }         });     }      public class testfilter extends documentfilter {          @override         public void remove(documentfilter.filterbypass fb, int offset, int length) throws badlocationexception {             system.out.println("remove");             super.remove(fb, offset, length);         }          @override         public void insertstring(documentfilter.filterbypass fb, int offset, string string, attributeset attr) throws badlocationexception {             system.out.println("insert");             super.insertstring(fb, offset, string, attr);         }          @override         public void replace(documentfilter.filterbypass fb, int offset, int length, string text, attributeset attrs) throws badlocationexception {             system.out.println("replace...");             super.replace(fb, offset, length, text, attrs);         }     } } 

strangely enough, example code worked me, problem had formatter


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -