/** * Returns format's pattern. * * @return format's pattern. */ public String getFormat() { if (format != null) return format; String fmt = null; if (formatter instanceof MaskFormatter) { fmt = ((MaskFormatter)formatter).getMask(); } else if (formatter instanceof InternationalFormatter) { Format f = ((InternationalFormatter)formatter).getFormat(); if (f instanceof DecimalFormat) { fmt = ((DecimalFormat)f).toPattern(); } else if (f instanceof SimpleDateFormat) { fmt = ((SimpleDateFormat)f).toPattern(); } } return fmt; }
/** * Returns the calendarField under the start of the selection, or -1 if * there is no valid calendar field under the selection (or the spinner * isn't editing dates. */ private int getCalendarField(JSpinner spinner) { JComponent editor = spinner.getEditor(); if( editor instanceof JSpinner.DateEditor ) { JSpinner.DateEditor dateEditor = (JSpinner.DateEditor) editor; JFormattedTextField ftf = dateEditor.getTextField(); int start = ftf.getSelectionStart(); JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter(); if( formatter instanceof InternationalFormatter ) { Format.Field[] fields = ((InternationalFormatter) formatter).getFields(start); for( int counter = 0; counter < fields.length; counter++ ) { if( fields[counter] instanceof DateFormat.Field ) { int calendarField; if( fields[counter] == DateFormat.Field.HOUR1 ) { calendarField = Calendar.HOUR; } else { calendarField = ((DateFormat.Field) fields[counter]).getCalendarField(); } if( calendarField != -1 ) { return calendarField; } } } } } return -1; }
/** * Returns the calendarField under the start of the selection, or * -1 if there is no valid calendar field under the selection (or * the spinner isn't editing dates. */ private int getCalendarField(final JSpinner spinnerComponent) { final JComponent editor = spinnerComponent.getEditor(); if (!(editor instanceof JSpinner.DateEditor)) return -1; final JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor; final JFormattedTextField ftf = dateEditor.getTextField(); final int start = ftf.getSelectionStart(); final JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter(); if (!(formatter instanceof InternationalFormatter)) return -1; final Format.Field[] fields = ((InternationalFormatter)formatter).getFields(start); for (final Field element : fields) { if (!(element instanceof DateFormat.Field)) continue; int calendarField; if (element == DateFormat.Field.HOUR1) { calendarField = Calendar.HOUR; } else { calendarField = ((DateFormat.Field)element).getCalendarField(); } if (calendarField != -1) { return calendarField; } } return -1; }
public TestFormattedTextField(InternationalFormatter fmt) { super(fmt); fmt.setAllowsInvalid(false); fmt.setOverwriteMode(true); backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction); delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction); insert = getActionMap().get(DefaultEditorKit.insertContentAction); dummyEvent = new ActionEvent(this, 0, null); }
/** * Returns the calendarField under the start of the selection, or -1 if * there is no valid calendar field under the selection (or the spinner * isn't editing dates. */ private int getCalendarField(final JSpinner spinnerComponent) { final JComponent editor = spinnerComponent.getEditor(); if (!(editor instanceof JSpinner.DateEditor)) { return -1; } final JSpinner.DateEditor dateEditor = (JSpinner.DateEditor) editor; final JFormattedTextField ftf = dateEditor.getTextField(); final int start = ftf.getSelectionStart(); final JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter(); if (!(formatter instanceof InternationalFormatter)) { return -1; } final Format.Field[] fields = ((InternationalFormatter) formatter).getFields(start); for (final Field element : fields) { if (!(element instanceof DateFormat.Field)) { continue; } int calendarField; if (element == DateFormat.Field.HOUR1) { calendarField = Calendar.HOUR; } else { calendarField = ((DateFormat.Field) element).getCalendarField(); } if (calendarField != -1) { return calendarField; } } return -1; }
/** * Returns an AbstractFormatterFactory suitable for the passed in Object * type. */ private AbstractFormatterFactory getDefaultFormatterFactory(Object type) { if (type instanceof DateFormat) { return new DefaultFormatterFactory(new DateFormatter( (DateFormat) type)); } if (type instanceof NumberFormat) { return new DefaultFormatterFactory(new NumberFormatter( (NumberFormat) type)); } if (type instanceof Format) { return new DefaultFormatterFactory(new InternationalFormatter( (Format) type)); } if (type instanceof Date) { return new DefaultFormatterFactory(new DateFormatter()); } if (type instanceof Number) { AbstractFormatter displayFormatter = new NumberFormatter(); ((NumberFormatter) displayFormatter).setValueClass(type.getClass()); AbstractFormatter editFormatter = new NumberFormatter( new DecimalFormat("#.#")); ((NumberFormatter) editFormatter).setValueClass(type.getClass()); return new DefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter); } return new DefaultFormatterFactory(new DefaultFormatter()); }
public RangeTableEditor() { InternationalFormatter nf = new InternationalFormatter(); nf.setMinimum( 1 ); nf.setAllowsInvalid( true ); mEditor = new JFormattedTextField( nf ); }
@Override public AbstractFormatter getFormatter(JFormattedTextField tf) { NumberFormat format = DecimalFormat.getInstance(); // format.setMinimumIntegerDigits(0); format.setMinimumFractionDigits(1); //set minimum decimal place format.setMaximumFractionDigits(maximumFractionDigits); //set maximum decimal place format.setRoundingMode(RoundingMode.HALF_UP); //set rounding decimal method InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); return formatter; }
private AbstractFormatter createFormatter(final Format format) { if (format instanceof DateFormat) { return new DateFormatter((DateFormat) format); } else if (format instanceof NumberFormat) { return new NumberFormatter((NumberFormat) format); } else { return new InternationalFormatter(format); } }
public void testJFormattedTextFieldAbstractFormatter() { InternationalFormatter formatter = new InternationalFormatter(); JFormattedTextField tf1 = new JFormattedTextField(formatter); assertNull(tf1.getValue()); assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf1.getFocusLostBehavior()); assertEquals(formatter, tf1.getFormatter()); assertTrue(tf1.getFormatterFactory() instanceof DefaultFormatterFactory); DefaultFormatterFactory factory = (DefaultFormatterFactory) tf1.getFormatterFactory(); assertNull(factory.getDisplayFormatter()); assertNull(factory.getEditFormatter()); assertEquals(formatter, factory.getDefaultFormatter()); assertNull(factory.getNullFormatter()); }
/** * Issue ??-swingx: editor with strict number formatter throws on * committing null value. * * happens only if active editor in table. Use non-strict for comparison. * InternationalFormatter with bounds throws as well. But not using * strict catches this in isValid, that is stopCellEditing returns false. */ @Test public void testEditorNullValue() { table.getColumn(INTEGER_COLUMN).setCellEditor(cellEditor); ((InternationalFormatter) cellEditor.getComponent().getFormatter()).setMinimum(0); table.editCellAt(0, INTEGER_COLUMN); assertTrue(cellEditor.stopCellEditing()); }