if (maskTextField.getFormatter() instanceof DefaultFormatter) { DefaultFormatter f = (DefaultFormatter) maskTextField.getFormatter(); f.setAllowsInvalid(true); }
/** * Sets whether or not the value being edited is allowed to be invalid for a length of time (that is, * <code>stringToValue</code> throws a <code>ParseException</code>). It is often convenient to allow the user to * temporarily input an invalid value. * * @param allowsInvalid Used to indicate if the edited value must always be valid */ public void setAllowsInvalid(boolean allowsInvalid) { _formatter.setAllowsInvalid(allowsInvalid); }
JTextComponent txt = new JFormattedTextField( new PositiveIntegerFormatter() ); txt.addPropertyChangeListener("value", yourPropertyChangeListener); import javax.swing.text.DefaultFormatter; import java.text.ParseException; public class PositiveIntegerFormatter extends DefaultFormatter { static final long serialVersionUID = 1l; public PositiveIntegerFormatter() { setValueClass(Integer.class); setAllowsInvalid(false); setCommitsOnValidEdit(true); } @Override public Object stringToValue(String string) throws ParseException { if (string.equals("")) return null; Integer value = (Integer)super.stringToValue(string); if ( Integer.signum(value.intValue()) < 0 ) throw new ParseException(string, 0); return value; } }
JTextComponent txt = new JFormattedTextField( new LimitedIntegerFormatter(limit) ); txt.addPropertyChangeListener("value", yourPropertyChangeListener); import javax.swing.text.DefaultFormatter; import java.text.ParseException; public class LimitedIntegerFormatter extends DefaultFormatter { static final long serialVersionUID = 1l; private int limit; public LimitedIntegerFormatter( int limit ) { this.limit = limit; setValueClass(Integer.class); setAllowsInvalid(false); setCommitsOnValidEdit(true); } @Override public Object stringToValue(String string) throws ParseException { if (string.equals("")) return null; if (string.length() > limit) throw new ParseException(string, limit); return super.stringToValue(string); } }
formatter.setMinimum(0.0); formatter.setMaximum(10000000.0); formatter.setAllowsInvalid(false);
setMask("##/##/####"); setPlaceholderCharacter('0'); setAllowsInvalid(false); setOverwriteMode(true); } catch (ParseException e) {
ccyTxtFormat.setMinimumFractionDigits(0); NumberFormatter ccyTxtFormatter = new NumberFormatter(ccyTxtFormat); ccyTxtFormatter.setAllowsInvalid(true); DefaultFormatterFactory CcyTxtDecimalFormatterFactory = new DefaultFormatterFactory(ccyTxtFormatter); ccyTxt.setFormatterFactory(CcyTxtDecimalFormatterFactory);
formatter.setAllowsInvalid(false); // this makes what you want formatter.setOverwriteMode(true);
format.setRoundingMode(RoundingMode.HALF_UP); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); formatter.setMinimum(0.0); formatter.setMaximum(1000.00);
formatter = new MaskFormatter(format); formatter.setPlaceholderCharacter('.'/*or '0' etc*/); formatter.setAllowsInvalid(false); // if needed formatter.setOverwriteMode(true); // if needed } catch (java.text.ParseException exc) {
format.setRoundingMode(RoundingMode.HALF_UP); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); formatter.setMinimum(0.0); formatter.setMaximum(9000.00);
formatter.setMaximum(1000); formatter.setMinimum(0.0); formatter.setAllowsInvalid(false);
formatter.setMinimum(1); formatter.setMaximum(100); formatter.setAllowsInvalid(false); formatter.setOverwriteMode(true);
private static XSpinner createIntegerField() { final XSpinner spinner = new XSpinner(); JTextField editor = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField(); editor.setColumns(10); spinner.setUI(new javax.swing.plaf.basic.BasicSpinnerUI() { @Override protected Component createNextButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } @Override protected Component createPreviousButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } }); spinner.setBorder(null); ((DefaultFormatter) ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField().getFormatter()) .setAllowsInvalid(false); ((JComponent) editor.getParent()).setBorder(UIManager.getBorder("TextField.border")); return spinner; }
private static XSpinner createIntegerField() { final XSpinner spinner = new XSpinner(); JTextField editor = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField(); editor.setColumns(10); spinner.setUI(new javax.swing.plaf.basic.BasicSpinnerUI() { @Override protected Component createNextButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } @Override protected Component createPreviousButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } }); spinner.setBorder(null); ((DefaultFormatter) ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField().getFormatter()) .setAllowsInvalid(false); ((JComponent) editor.getParent()).setBorder(UIManager.getBorder("TextField.border")); return spinner; }
private static XSpinner createIntegerField() { final XSpinner spinner = new XSpinner(); JTextField editor = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField(); editor.setColumns(10); spinner.setUI(new javax.swing.plaf.basic.BasicSpinnerUI() { @Override protected Component createNextButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } @Override protected Component createPreviousButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } }); spinner.setBorder(null); ((DefaultFormatter) ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField().getFormatter()) .setAllowsInvalid(false); ((JComponent) editor.getParent()).setBorder(UIManager.getBorder("TextField.border")); return spinner; }
private static XSpinner createIntegerField() { final XSpinner spinner = new XSpinner(); JTextField editor = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField(); editor.setColumns(10); spinner.setUI(new javax.swing.plaf.basic.BasicSpinnerUI() { @Override protected Component createNextButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } @Override protected Component createPreviousButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } }); spinner.setBorder(null); ((DefaultFormatter) ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField().getFormatter()) .setAllowsInvalid(false); ((JComponent) editor.getParent()).setBorder(UIManager.getBorder("TextField.border")); return spinner; }
private static XSpinner createIntegerField() { final XSpinner spinner = new XSpinner(); JTextField editor = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField(); editor.setColumns(10); spinner.setUI(new javax.swing.plaf.basic.BasicSpinnerUI() { @Override protected Component createNextButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } @Override protected Component createPreviousButton() { Component c = new JButton(); c.setPreferredSize(new Dimension(0, 0)); c.setFocusable(false); return c; } }); spinner.setBorder(null); ((DefaultFormatter) ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField().getFormatter()) .setAllowsInvalid(false); ((JComponent) editor.getParent()).setBorder(UIManager.getBorder("TextField.border")); return spinner; }
format.setRoundingMode(RoundingMode.HALF_UP); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); formatter.setMinimum(0.0); formatter.setMaximum(1000.00); format.setRoundingMode(RoundingMode.HALF_UP); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false);
formatter.setMaximum(1000.0); formatter.setMinimum(0.0); formatter.setAllowsInvalid(false); formatter.setOverwriteMode(false);