This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
o New flavor of stackoverflow approach for number-only JSpinners o JSpinners don't fire focus events, so use ChangeListener instead o Actually ran ./gradlew test check this time (sorry)
- Loading branch information
1 parent
ec92ba6
commit 7c782ba
Showing
3 changed files
with
23 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,24 @@ | ||
package org.opendatakit.briefcase.ui; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.text.DecimalFormat; | ||
|
||
import javax.swing.JFormattedTextField; | ||
import javax.swing.JSpinner; | ||
import javax.swing.SpinnerNumberModel; | ||
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; | ||
import javax.swing.text.NumberFormatter; | ||
|
||
//Sources: | ||
//https://stackoverflow.com/questions/16632104/jspinner-with-display-format-numbers-only-and-manual-edit | ||
//http://stackoverflow.com/questions/20541230/allow-only-numbers-in-jtextfield | ||
//https://stackoverflow.com/questions/6449350/make-jspinner-completely-numeric | ||
public class JIntegerSpinner extends JSpinner { | ||
|
||
public JIntegerSpinner(int value, int min, int max, int step) { | ||
super (new SpinnerNumberModel(8080, 0, 65535, 1)); | ||
JTextComponent txt = ((JSpinner.DefaultEditor) this.getEditor()).getTextField(); | ||
txt.setDocument(new IntegerDocument()); | ||
} | ||
|
||
private class IntegerDocument extends PlainDocument { | ||
private IntegerDocumentFilter filter; | ||
|
||
protected IntegerDocument() { | ||
filter = new IntegerDocumentFilter(); | ||
} | ||
@Override | ||
public DocumentFilter getDocumentFilter() { | ||
return filter; | ||
} | ||
} | ||
|
||
private class IntegerDocumentFilter extends DocumentFilter { | ||
@Override | ||
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { | ||
Pattern regEx = Pattern.compile("\\d*"); | ||
Matcher matcher = regEx.matcher(text); | ||
if(!matcher.matches()){ | ||
return; | ||
} | ||
super.replace(fb, offset, length, text, attrs); | ||
} | ||
JFormattedTextField txt = ((JSpinner.NumberEditor) this.getEditor()).getTextField(); | ||
NumberFormatter formatter = (NumberFormatter)txt.getFormatter(); | ||
formatter.setFormat(new DecimalFormat("#####")); | ||
formatter.setAllowsInvalid(false); | ||
((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false); | ||
txt.setValue(value); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters