/** * Creates a {@link NumberFormatter} for display-mode from a given {@link NumberFormat}. * * @param format for the formatter * @return created {@link NumberFormatter} * @see NumberFormat * @see NumberFormatter * @since 0.0.1 */ public static NumberFormatter createNumberFormatterDisplay(final NumberFormat format) { if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(format)); if (null == format) { throw new RuntimeExceptionIsNull("format"); //$NON-NLS-1$ } final NumberFormatter result = new NumberFormatter(format) { private static final long serialVersionUID = -8279122736069976223L; { setCommitsOnValidEdit(true); } }; if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result)); return result; }
private void initializeTextFields() { DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); NumberFormat format = new DecimalFormat("0.00", symbols); format.setMaximumFractionDigits(2); NumberFormatter formatter = new NumberFormatter(format); formatter.setMinimum(0.0); formatter.setMaximum(10000000.0); formatter.setAllowsInvalid(false); this.txtPeso = new JFormattedTextField(formatter); this.txtPeso.setValue(0.0); GridBagConstraints gbc_textField = new GridBagConstraints(); gbc_textField.gridwidth = 10; gbc_textField.insets = new Insets(0, 0, 5, 0); gbc_textField.fill = GridBagConstraints.BOTH; gbc_textField.gridx = 2; gbc_textField.gridy = 10; this.panelSecond.add(this.txtPeso, gbc_textField); this.txtPeso.setColumns(10); }
/** * Creates a new WettDialog and displays the given array of snails. * @param parent The Form opening this dialog * @param schneggen The array of snails to display * @param wettbueroFactor The factor the Wettbuero uses. */ public WettDialog(Frame parent, ArrayList<Rennschnecke> schneggen, double wettbueroFactor) { super(parent, true); initComponents(); NumberFormat format = NumberFormat.getCurrencyInstance(); format.setMinimumFractionDigits(2); format.setMaximumFractionDigits(2); NumberFormatter nf = new NumberFormatter(format); nf.setMinimum(0.02); // The maximum bet value is, well, pretty high. nf.setMaximum(Double.MAX_VALUE / wettbueroFactor); nf.setAllowsInvalid(false); nf.setCommitsOnValidEdit(true); nf.setOverwriteMode(false); einsatzInput.setFormatterFactory( new DefaultFormatterFactory(nf) ); result = null; snailList.setModel(new DefaultListModel<>()); snailList.setListData(schneggen.toArray(new Rennschnecke[schneggen.size()])); }
public NumberPropertyEditor(Class type) { if (!Number.class.isAssignableFrom(type)) { throw new IllegalArgumentException("type must be a subclass of Number"); } editor = new JFormattedTextField(); this.type = type; ((JFormattedTextField)editor).setValue(getDefaultValue()); ((JFormattedTextField)editor).setBorder(LookAndFeelTweaks.EMPTY_BORDER); // use a custom formatter to have numbers with up to 64 decimals NumberFormat format = NumberConverters.getDefaultFormat(); ((JFormattedTextField) editor).setFormatterFactory( new DefaultFormatterFactory(new NumberFormatter(format)) ); }
/** * Construct a <code>JSpinner</code> editor that supports displaying and * editing the value of a <code>SpinnerIntModel</code> with a * <code>JFormattedTextField</code>. <code>This</code> * <code>IntegerEditor</code> becomes both a <code>ChangeListener</code> * on the spinner and a <code>PropertyChangeListener</code> on the new * <code>JFormattedTextField</code>. * * @param spinner the spinner whose model <code>this</code> editor will * monitor * @param format the <code>NumberFormat</code> object that's used to * display and parse the value of the text field. * @exception IllegalArgumentException if the spinners model is not an * instance of <code>SpinnerIntModel</code> * * @see #getTextField * @see SpinnerIntModel * @see java.text.DecimalFormat */ private IntegerEditor(JSpinner spinner, NumberFormat format) { super(spinner); if (!(spinner.getModel() instanceof SpinnerIntModel)) { throw new IllegalArgumentException("model not a SpinnerIntModel"); } format.setGroupingUsed(false); format.setMaximumFractionDigits(0); SpinnerIntModel model = (SpinnerIntModel) spinner.getModel(); NumberFormatter formatter = new IntegerEditorFormatter(model, format); DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter); JFormattedTextField ftf = getTextField(); ftf.setEditable(true); ftf.setFormatterFactory(factory); ftf.setHorizontalAlignment(JTextField.RIGHT); try { String minString = formatter.valueToString(model.getMinimum()); String maxString = formatter.valueToString(model.getMaximum()); // Trying to approximate the width difference between "m" and "0" by multiplying with 0.7 ftf.setColumns((int) Math.round(0.7 * Math.max(maxString.length(), minString.length()))); } catch (ParseException e) { // Nothing to do, the component width will simply be the default } }
public static String formatNumber(Number num, int digits) { String s = "0."; for (int i = 0; i < digits; i++) { s += "0"; } DecimalFormat df = new DecimalFormat(s); NumberFormatter nf = new NumberFormatter(df); String out = "Illegal Format"; try { out = nf.valueToString(num); } catch (ParseException e) { } return out; }
public String getValorTitulo() { String zeros = "0000000000"; DefaultFormatter formatter = new NumberFormatter(new DecimalFormat("#,##0.00")); String valor = ""; try { valor = formatter.valueToString(getValorTituloBigDecimal()); } catch (Exception ex) { } valor = valor.replace(",", "").replace(".", ""); String valorTitulo = zeros.substring(0, zeros.length() - valor.length()) + valor; return valorTitulo; }
public JNumberField(double defaultNumber, double minValue, double maxValue, double increment) { super(new SpinnerNumberModel(defaultNumber, minValue, maxValue, increment)); final NumberEditor editor = (NumberEditor) this.getEditor(); editor.getFormat().setMinimumFractionDigits(2); editor.getFormat().setMaximumFractionDigits(6); final JFormattedTextField spinnerTextEditor = editor.getTextField(); spinnerTextEditor.setColumns(6); spinnerTextEditor.setHorizontalAlignment(JTextField.CENTER); final NumberFormatter numberFormatter = new NumberFormatter(editor.getFormat()); numberFormatter.setValueClass(Double.class); numberFormatter.setMinimum(0d); numberFormatter.setMaximum(Double.MAX_VALUE); numberFormatter.setAllowsInvalid(false); spinnerTextEditor.setFormatterFactory(new JFormattedTextField(numberFormatter).getFormatterFactory()); }
private JPanel getImageSizePanel() { JPanel imageSizePanel = new JPanel(); imageSizePanel.setLayout(new GridLayout(1, 4)); NumberFormat format = NumberFormat.getInstance(); format.setGroupingUsed(false); NumberFormatter formatter = new NumberFormatter(format); formatter.setValueClass(Integer.class); formatter.setMinimum(0); formatter.setMaximum(Integer.MAX_VALUE); formatter.setCommitsOnValidEdit(true); widthTF = new JFormattedTextField(formatter); widthTF.setValue(1200); addFocusListener(widthTF); heightTF = new JFormattedTextField(formatter); heightTF.setValue(1000); addFocusListener(heightTF); imageSizePanel.add(new JLabel(" Width:")); imageSizePanel.add(widthTF); imageSizePanel.add(new JLabel(" Height:")); imageSizePanel.add(heightTF); return imageSizePanel; }
private JPanel getImageSizePanel() { JPanel imageSizePanel = new JPanel(); imageSizePanel.setLayout(new GridLayout(1, 4)); NumberFormat format = NumberFormat.getInstance(); format.setGroupingUsed(false); NumberFormatter formatter = new NumberFormatter(format); formatter.setValueClass(Integer.class); formatter.setMinimum(0); formatter.setMaximum(Integer.MAX_VALUE); formatter.setCommitsOnValidEdit(true); widthTF = new JFormattedTextField(formatter); widthTF.setValue(1200); widthTF.addFocusListener(FOCUS_LISTENER); heightTF = new JFormattedTextField(formatter); heightTF.setValue(1000); heightTF.addFocusListener(FOCUS_LISTENER); imageSizePanel.add(new JLabel(" Width:")); imageSizePanel.add(widthTF); imageSizePanel.add(new JLabel(" Height:")); imageSizePanel.add(heightTF); return imageSizePanel; }
public JFormattedTextField getIntegerTextField(Integer min, Integer max, Integer value) { NumberFormat format = NumberFormat.getInstance(); NumberFormatter formatter = new NumberFormatter(format); formatter.setValueClass(Integer.class); formatter.setMinimum(min); formatter.setMaximum(max); formatter.setAllowsInvalid(true); formatter.setCommitsOnValidEdit(false); JFormattedTextField field = new JFormattedTextField(formatter); field.setValue(value); field.setFont(REGULAR_FONT.deriveFont(scale * 18)); field.setFocusLostBehavior(JFormattedTextField.PERSIST); field.setForeground(AppThemeColor.TEXT_DEFAULT); field.setCaretColor(AppThemeColor.TEXT_DEFAULT); field.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(AppThemeColor.BORDER, 1), BorderFactory.createLineBorder(AppThemeColor.TRANSPARENT, 3) )); field.setBackground(AppThemeColor.HEADER); return field; }
public JIntegerTextField() { super(); DecimalFormat df = new DecimalFormat("####"); nf = new NumberFormatter(df) { @Override public String valueToString(Object iv) throws ParseException { if ((iv == null) || (((Number) iv).intValue() == -1)) { return ""; } return super.valueToString(iv); } @Override public Object stringToValue(String text) throws ParseException { if ("".equals(text)) { return null; } return super.stringToValue(text); } }; nf.setValueClass(Integer.class); this.setFormatter(nf); }
/** * Sets integer number format to JFormattedTextField instance, * sets value of JFormattedTextField instance to object's field value, * synchronizes object's field value with the value of JFormattedTextField instance. * * @param textField JFormattedTextField instance * @param owner an object whose field is synchronized with {@code textField} * @param property object's field name for synchronization */ public static void setupIntegerFieldTrackingValue(final JFormattedTextField textField, final InspectionProfileEntry owner, final String property) { NumberFormat formatter = NumberFormat.getIntegerInstance(); formatter.setParseIntegerOnly(true); textField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(formatter))); textField.setValue(getPropertyValue(owner, property)); final Document document = textField.getDocument(); document.addDocumentListener(new DocumentAdapter() { @Override public void textChanged(DocumentEvent e) { try { textField.commitEdit(); setPropertyValue(owner, property, ((Number) textField.getValue()).intValue()); } catch (ParseException e1) { // No luck this time } } }); }
/** * Constructor. * @param property The property which should be edited. * @param configuration The configuration which should be edited. * @param client YouScope client * @param server YouScope server. * @throws GenericException */ public PropertyDoubleEditor(Property property, Configuration configuration, YouScopeClient client, YouScopeServer server) throws GenericException { super(property, configuration, client, server, Double.class, double.class); field = new JFormattedTextField(new NumberFormatter(getDoubleFormat())); field.setValue(getValue(Double.class)); addLabel(); add(field); field.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { commitEdits(); notifyPropertyValueChanged(); } catch (GenericException e1) { sendErrorMessage("Could not set value of property " + getProperty().getName() + ".", e1); } } }); }
public NumberPropertyEditor(Class<?> type) { if (!Number.class.isAssignableFrom(type)) { throw new IllegalArgumentException("type must be a subclass of Number"); } editor = new JFormattedTextField(); this.type = type; ((JFormattedTextField) editor).setValue(getDefaultValue()); ((JFormattedTextField) editor).setBorder(LookAndFeelTweaks.EMPTY_BORDER); // use a custom formatter to have numbers with up to 64 decimals NumberFormat format = NumberConverters.DEFAULT_FORMAT; ((JFormattedTextField) editor).setFormatterFactory( new DefaultFormatterFactory(new NumberFormatter(format)) ); }
/** * Erzeugt ein Anzeigefeld fuer den Simulatortakt */ @SuppressWarnings("boxing") void initTickRateField() { NumberFormatter formatter = new NumberFormatter(NumberFormat.getInstance()); formatter.setMinimum(StatusBar.MIN_TICK_RATE); formatter.setMaximum(StatusBar.MAX_TICK_RATE); tickRateField = new JFormattedTextField(formatter); tickRateField.setValue(INIT_TICK_RATE); tickRateField.setColumns(3); tickRateField.setHorizontalAlignment(SwingConstants.CENTER); tickRateField.setMinimumSize(new Dimension(44, 22)); tickRateField.setMaximumSize(new Dimension(44, 22)); tickRateField.setPreferredSize(new Dimension(44, 22)); tickRateField.addPropertyChangeListener(new PropertyChangeListener() { @SuppressWarnings("synthetic-access") public void propertyChange(PropertyChangeEvent evt) { if ("value".equals(evt.getPropertyName())) { tickRateSlider.setValue((Integer) tickRateField.getValue()); } } }); }
private void initFormats() { nfInteger = NumberFormat.getIntegerInstance(); NumberFormatter nf = new NumberFormatter(nfInteger); nf.setMaximum(Server.MAX_PORT_INDEX); nf.setMinimum(0); NumberFormat editFormat = NumberFormat.getIntegerInstance(); editFormat.setGroupingUsed(false); NumberFormatter editFormatter = new NumberFormatter(editFormat); editFormatter.setMaximum(Server.MAX_PORT_INDEX); editFormatter.setMinimum(0); integerFormatter = new DefaultFormatterFactory(nf, nf, editFormatter, nf); port.setFormatterFactory(integerFormatter); port.setValue(28015); refreshFrequency.setFormatterFactory(integerFormatter); refreshFrequency.setValue(1); }
public BigIntegerEditor() { Class t = BigInteger.class; if (!Number.class.isAssignableFrom(t)) { throw new IllegalArgumentException("type must be a subclass of Number"); } editor = new JFormattedTextField(); this.type = t; ((JFormattedTextField) editor).setValue(getDefaultValue()); ((JFormattedTextField) editor).setBorder(LookAndFeelTweaks.EMPTY_BORDER); // use a custom formatter to have numbers with up to 64 decimals format = NumberConverters.getDefaultFormat(); ((JFormattedTextField) editor).setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(format))); }
public BigDecimalEditor() { Class t = BigDecimal.class; if (!Number.class.isAssignableFrom(t)) { throw new IllegalArgumentException("type must be a subclass of Number"); } editor = new JFormattedTextField(); this.type = t; ((JFormattedTextField) editor).setValue(getDefaultValue()); ((JFormattedTextField) editor).setBorder(LookAndFeelTweaks.EMPTY_BORDER); // use a custom formatter to have numbers with up to 64 decimals format = NumberConverters.getDefaultFormat(); ((JFormattedTextField) editor).setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(format))); }
public static Dimension editStackSize(Dimension currentSize, Component parent) { StackSizeEditor dialog = new StackSizeEditor(); dialog.stackDimension = currentSize; NumberFormat format = NumberFormat.getInstance(); NumberFormatter formatter = new NumberFormatter(format); formatter.setValueClass(Integer.class); formatter.setMinimum(0); formatter.setMaximum(Integer.MAX_VALUE); formatter.setAllowsInvalid(false); formatter.setCommitsOnValidEdit(true); dialog.newHeight.setFormatterFactory(new DefaultFormatterFactory(formatter)); dialog.newWidth.setFormatterFactory(new DefaultFormatterFactory(formatter)); dialog.currentStackSize.setText("Stack is " + currentSize.width + "px by " + currentSize.height + "px"); dialog.newWidth.setText(String.valueOf(currentSize.width)); dialog.newHeight.setText(String.valueOf(currentSize.height)); dialog.pack(); dialog.setLocationRelativeTo(parent); dialog.setVisible(true); return dialog.stackDimension; }
/** * Creates new form InstanceSelection */ public InstanceSelectionPanel(ProviderService providerService) { this.providerService_ = providerService; this.providers_ = providerService_.providers(); this.regions_ = providerService_.allRegions(); initComponents(); ((NumberFormatter) ((JSpinner.NumberEditor) spnCpu.getEditor()).getTextField().getFormatter()).setAllowsInvalid(false); ((NumberFormatter) ((JSpinner.NumberEditor) spnFlops.getEditor()).getTextField().getFormatter()).setAllowsInvalid(false); ((NumberFormatter) ((JSpinner.NumberEditor) spnMemory.getEditor()).getTextField().getFormatter()).setAllowsInvalid(false); ((NumberFormatter) tfCost.getFormatter()).setAllowsInvalid(false); this.lblMessage.setVisible(false); this.btnRecommend.setVisible(false); }
/** * Constructor. * Sets the DefaultFormatterFactory created based on {@link * NumberEditorFormatter} in the text field. * @param spinner the spinner for which this editor is used * @param format the format of the numbers shown in the spinner */ public SpinnerNumberEditor(JSpinner spinner, DecimalFormat format) { super(spinner); if (!(spinner.getModel() instanceof BoundedSpinnerModel)) { throw new IllegalArgumentException( "model not a BoundedSpinnerModel"); } BoundedSpinnerModel model = (BoundedSpinnerModel) spinner.getModel(); NumberFormatter formatter = new NumberEditorFormatter(model, format); DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter); JFormattedTextField ftf = getTextField(); ftf.setEditable(true); ftf.setFormatterFactory(factory); ftf.setHorizontalAlignment(JTextField.RIGHT); try { String maxString = formatter.valueToString(model.getMinimum()); String minString = formatter.valueToString(model.getMaximum()); ftf.setColumns(Math.max(maxString.length(),minString.length())); } catch (ParseException e) { } }
public void testCreateFormattersFactory() { DefaultFormatterFactory factory; tf.setValue(new Integer(34)); factory = getFactoryIfDefault(tf.getFormatterFactory()); assertTrue(factory.getDefaultFormatter() instanceof NumberFormatter); //TODO: check if factory.getDefaultFormatter() should be same to factory.getDisplayFormatter() // or factory.getEditFormatter(). assertNull(factory.getNullFormatter()); tf.setFormatterFactory(null); tf.setValue(new Date()); factory = getFactoryIfDefault(tf.getFormatterFactory()); assertTrue(factory.getDefaultFormatter() instanceof DateFormatter); assertNull(factory.getDisplayFormatter()); assertNull(factory.getEditFormatter()); assertNull(factory.getNullFormatter()); tf.setFormatterFactory(null); tf.setValue("sdffsdf"); factory = getFactoryIfDefault(tf.getFormatterFactory()); checkDefaultFormatter(factory); tf.setFormatterFactory(null); tf.setValue(Color.RED); factory = getFactoryIfDefault(tf.getFormatterFactory()); checkDefaultFormatter(factory); }
public void testNumberEditor_NumberEditor() { spinner.getModel().setValue(new Integer(5)); NumberEditor numEditor = new NumberEditor(spinner); spinner.setEditor(numEditor); assertTrue(numEditor.getTextField().isEditable()); assertTrue(numEditor.getTextField().getFormatter() instanceof NumberFormatter); assertEquals(numEditor.getTextField().getValue(), new Integer(5)); assertSame(((NumberFormatter) numEditor.getTextField().getFormatter()).getValueClass(), Integer.class); assertNull(((NumberFormatter) numEditor.getTextField().getFormatter()).getMinimum()); assertNull(((NumberFormatter) numEditor.getTextField().getFormatter()).getMaximum()); assertTrue(numEditor.getFormat().equals(new DecimalFormat())); spinner.setModel(abstractModel); testExceptionalCase(new IllegalArgumentCase() { @Override public void exceptionalAction() throws Exception { new JSpinner.NumberEditor(spinner); } }); }
public void testNumberEditor_formatter() { NumberEditor numEditor = new NumberEditor(spinner); spinner.setEditor(numEditor); final Integer max1 = new Integer(777); NumberFormatter numberFormatter = ((NumberFormatter) numEditor.getTextField() .getFormatter()); numberFormatter.setMaximum(max1); assertSame(numberFormatter.getMaximum(), max1); assertSame(numEditor.getModel().getMaximum(), max1); final Integer max2 = new Integer(555); numEditor.getModel().setMaximum(max2); assertSame(numberFormatter.getMaximum(), max2); assertSame(numEditor.getModel().getMaximum(), max2); SpinnerNumberModel old = (SpinnerNumberModel) spinner.getModel(); spinner.setModel(abstractModel); final Integer max3 = new Integer(333); old.setMaximum(max3); assertSame(((NumberFormatter) ((NumberEditor) spinner.getEditor()).getTextField() .getFormatter()).getMaximum(), max3); }
private void setLookAndFeel() { setOpaque(false); setLayout(migLayout); setPreferredSize(new Dimension(0, 30)); // filter combo numericFilterCombo.setModel(new DefaultComboBoxModel<>(NumericFilter.values())); // spinner1 sizeSpinner1.setVisible(false); // allow only numeric characters to be recognised. sizeSpinner1.setEditor(new JSpinner.NumberEditor(sizeSpinner1,"#")); final JFormattedTextField txt1 = ((JSpinner.NumberEditor) sizeSpinner1.getEditor()).getTextField(); ((NumberFormatter)txt1.getFormatter()).setAllowsInvalid(false); // spinner2 sizeSpinner2.setVisible(false); // allow only numeric characters to be recognised. sizeSpinner2.setEditor(new JSpinner.NumberEditor(sizeSpinner2,"#")); final JFormattedTextField txt2 = ((JSpinner.NumberEditor) sizeSpinner2.getEditor()).getTextField(); ((NumberFormatter)txt2.getFormatter()).setAllowsInvalid(false); }
/** * Constructs a formatter that will use the given values when formatting byte values. * * @param gigabyteSuffix * string to display at the end of gigabyte values. * @param megabyteSuffix * string to display at the end of megabyte values. * @param kilobyteSuffix * string to display at the end of kilobyte values. * @param byteSuffix * string to display at the end of byte values. * @param decimalPlaces * the number of decimal places to use when converting byte amounts into kilo, mega or giga * byte values. */ public ByteFormatter(String gigabyteSuffix, String megabyteSuffix, String kilobyteSuffix, String byteSuffix, int decimalPlaces) { this.gigabyteSuffix = gigabyteSuffix; this.megabyteSuffix = megabyteSuffix; this.kilobyteSuffix = kilobyteSuffix; this.byteSuffix = byteSuffix; StringBuilder numberFormatString = new StringBuilder(); numberFormatString.append("0").append((decimalPlaces > 0? "." : "")); for (int i = 0; i < decimalPlaces; i++) { numberFormatString.append("0"); } nf = new NumberFormatter(new DecimalFormat(numberFormatString.toString())); }
/** * @param min minimum value * @param max maximum value * @return text field for editing integers */ public static JFormattedTextField createIntegerField(final int min, final int max) { final NumberFormatter def = new NumberFormatter(); def.setValueClass(Integer.class); final NumberFormatter disp = new NumberFormatter((new DecimalFormat("#,###,##0"))); disp.setValueClass(Integer.class); final NumberFormatter ed = new NumberFormatter((new DecimalFormat("#,###,##0"))); ed.setValueClass(Integer.class); final DefaultFormatterFactory factory = new DefaultFormatterFactory(def, disp, ed); final JFormattedTextField field = new JFormattedTextField(factory); field.setValue(Integer.valueOf(min)); field.setInputVerifier(new IntegerVerifier(min, max)); return field; }