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; }
@Override public void setCommitOnValidEdit(final boolean commit) { if (textComponent instanceof JFormattedTextField) { final JFormattedTextField formattedTextField = (JFormattedTextField)textComponent; final AbstractFormatter formatter = formattedTextField.getFormatter(); if (formatter instanceof DefaultFormatter) { final DefaultFormatter defaultFormatter = (DefaultFormatter)formatter; defaultFormatter.setCommitsOnValidEdit(commit); return; } } else if (textComponent instanceof JTextArea) { // ignore it for now } else { throw new UnsupportedOperationException("setCommitOnValidValue not supported for " + this); } }
public NumberEditor(String fieldName, Object obj, Field field, int index, Class<?> type, SWFType swfType) { setSize(100, getSize().height); setMaximumSize(getSize()); this.obj = obj; this.field = field; this.index = index; this.type = type; this.swfType = swfType; this.fieldName = fieldName; reset(); JFormattedTextField jtf = ((JSpinner.NumberEditor) getEditor()).getTextField(); DefaultFormatter formatter = (DefaultFormatter) jtf.getFormatter(); formatter.setCommitsOnValidEdit(true); }
private void setSpinner() { spinnerAmount.setModel(new SpinnerNumberModel(new Float(0), new Float(0), new Float(Float.MAX_VALUE), new Float(1f))); spinnerAmount.setFont(new Font("Lucida Grande", Font.PLAIN, 15)); JTextField tf = ((JSpinner.DefaultEditor) spinnerAmount.getEditor()).getTextField(); tf.setFont(new Font("Lucida Grande", Font.PLAIN, 15)); tf.setHorizontalAlignment(SwingConstants.CENTER); JFormattedTextField field = (JFormattedTextField) spinnerAmount.getEditor().getComponent(0); DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); formatter.setCommitsOnValidEdit(true); JSpinner.NumberEditor editor = (JSpinner.NumberEditor)spinnerAmount.getEditor(); DecimalFormat format = editor.getFormat(); format.setMinimumFractionDigits(8); //tf.setFont(new Font("Lucida Grande", Font.PLAIN, 15)); //tf.setHorizontalAlignment(SwingConstants.CENTER); //DecimalFormat format = editor.getFormat(); //format.setMinimumFractionDigits(8); }
/** Creates a new instance of NumberField */ public NumberField() { // define a custom input verifier this.setInputVerifier(new NumberVerifier()); // create a formatter for displaying and editing DefaultFormatter formatter = new DefaultFormatter(); // allow the user to completely delete all text formatter.setAllowsInvalid(true); // typing should insert new characters and not overwrite old ones formatter.setOverwriteMode(false); // commit on edit, otherwise a property change event is generated // when the field loses the focus and the value changed since it gained // the focus. formatter.setCommitsOnValidEdit(true); // getValue should return a Double object formatter.setValueClass(java.lang.Double.class); // the kind of formatter getFormatter should return this.setFormatterFactory(new DefaultFormatterFactory(formatter)); // default value is 0 this.setValue(new Double(0)); }
private static void testDefaultFormatter(DefaultFormatter formatter ) { try { System.out.println("formatter: " + formatter.getClass()); formatter.setValueClass(UserValueClass.class); UserValueClass userValue = (UserValueClass) formatter.stringToValue("test"); if (!userValue.str.equals("test")) { throw new RuntimeException("String value is wrong!"); } } catch (ParseException ex) { throw new RuntimeException(ex); } }
private EditorField createField(String text, String prototype, String tooltip) { DefaultFormatter formatter = new DefaultFormatter(); formatter.setOverwriteMode(false); EditorField field = new EditorField(new DefaultFormatterFactory(formatter), this, SwingConstants.LEFT, text, prototype, tooltip); field.setEnabled(mIsEditable); add(field); return field; }
/** * Creates a new text field. * * @param parent The parent. * @param title The title of the field. * @param value The initial value. * @return The newly created field. */ protected EditorField createTextField(Container parent, String title, Object value) { DefaultFormatter formatter = new DefaultFormatter(); formatter.setOverwriteMode(false); EditorField field = new EditorField(new DefaultFormatterFactory(formatter), this, SwingConstants.LEFT, value, null); parent.add(new LinkedLabel(title, field)); parent.add(field); return field; }
/** * @param compare The current string compare object. * @return The field that allows a string comparison to be changed. */ protected EditorField addStringCompareField(StringCriteria compare) { DefaultFormatter formatter = new DefaultFormatter(); formatter.setOverwriteMode(false); EditorField field = new EditorField(new DefaultFormatterFactory(formatter), this, SwingConstants.LEFT, compare.getQualifier(), null); field.putClientProperty(StringCriteria.class, compare); add(field); return field; }
/** * Instantiates a new value spinner. */ public IntegerSpinner(int min, int max, int stepSize) { SpinnerNumberModel model = new SpinnerNumberModel(min, min, max, stepSize); setModel(model); JComponent comp = getEditor(); final JFormattedTextField field = (JFormattedTextField) comp.getComponent(0); DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); formatter.setCommitsOnValidEdit(true); addChangeListener(new ChangeListener() { private double oldValue = Double.MAX_VALUE; @Override public void stateChanged(ChangeEvent e) { Double doubleValue = IntegerSpinner.this.getDoubleValue(); if (doubleValue != oldValue) { double oldValueCopy = oldValue; oldValue = doubleValue; if (minIsZero) { if (doubleValue < 0.0) { doubleValue = 0.0; field.setValue(doubleValue); } } notifyListeners(oldValueCopy, doubleValue); } } }); }
/** * Creates the ui. * * @param initialValue the initial value * @param min the min * @param max the max * @param stepSize the step size * @param noOfDecimalPlaces the no of decimal places */ private void createUI(Double initialValue, Double min, Double max, Double stepSize, double noOfDecimalPlaces) { SpinnerNumberModel model = new SpinnerNumberModel(initialValue, min, max, stepSize); setModel(model); JSpinner.NumberEditor editor = (JSpinner.NumberEditor) getEditor(); DecimalFormat format = editor.getFormat(); format.setMinimumFractionDigits(3); final JFormattedTextField field = (JFormattedTextField) editor.getTextField(); DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); formatter.setCommitsOnValidEdit(true); addChangeListener(new ChangeListener() { private double oldValue = Double.MAX_VALUE; @Override public void stateChanged(ChangeEvent e) { Double doubleValue = DecimalSpinner.this.getDoubleValue(); if (doubleValue != oldValue) { double oldValueCopy = oldValue; oldValue = doubleValue; if (minIsZero) { if (doubleValue < 0.0) { doubleValue = 0.0; field.setValue(doubleValue); } } notifyListeners(oldValueCopy, doubleValue); } } }); }
/** * 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()); }
private AbstractFormatterFactory createFactory(final Object value) { DefaultFormatterFactory factory = new DefaultFormatterFactory(); if (value instanceof Number) { factory.setDefaultFormatter(new NumberFormatter()); } else if (value instanceof Date) { factory.setDefaultFormatter(new DateFormatter()); } else { factory.setDefaultFormatter(new DefaultFormatter()); } return factory; }
public void testJFormattedTextFieldObject() { Object value = Color.RED; JFormattedTextField tf1 = new JFormattedTextField(value); assertEquals(value, tf1.getValue()); assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf1.getFocusLostBehavior()); assertTrue(tf1.getFormatter() instanceof DefaultFormatter); assertTrue(tf1.getFormatterFactory() instanceof DefaultFormatterFactory); DefaultFormatterFactory factory = (DefaultFormatterFactory) tf1.getFormatterFactory(); assertTrue(factory.getDefaultFormatter() instanceof DefaultFormatter); assertNull(factory.getEditFormatter()); assertNull(factory.getDisplayFormatter()); assertNull(factory.getNullFormatter()); }
private void customInit() { setFocusLostBehavior(COMMIT); if (getFormatter() instanceof DefaultFormatter) { final DefaultFormatter d = (DefaultFormatter)getFormatter(); AbstractAction toggleOverwrite = new AbstractAction() { public void actionPerformed(ActionEvent e) { d.setOverwriteMode(!(d.getOverwriteMode())); } }; getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0), TOGGLE_OVERWRITE_MODE_ACTION); getActionMap().put(TOGGLE_OVERWRITE_MODE_ACTION, toggleOverwrite); } }
@Override protected QuestionPanel createComponent() { QuestionPanel panel = createDefaultQuestionPanel(wizardElement, componentController.getCatalogService()); textField = new JFormattedTextField( NumberFormat.getInstance()); if (wizardElement.isSetDefaultValue()) { textField.setValue(wizardElement.getDefaultValue()); } String formato; if (wizardElement.isSetMask()){ formato = wizardElement.getMask(); } else { formato = "###,###,##0.00"; } DefaultFormatter fmt = new NumberFormatter(new DecimalFormat(formato)); DefaultFormatterFactory fmtFactory = new DefaultFormatterFactory(fmt, fmt, fmt); textField.setFormatterFactory(fmtFactory); ((AbstractDocument) textField.getDocument()).setDocumentFilter(new PatternDocumentFilter(Pattern.compile("\\d+(?:,\\d{0,2})?"))); //add listener ActionListener listener = createDefaultListener(); if (listener!= null){ textField.addActionListener(listener); } panel.add(textField); return panel; }
/** * Creates a {@link DefaultFormatter} from a given regex pattern. * * @param regex for the formatter * @return created {@link DefaultFormatter} * @see Pattern * @see DefaultFormatter * @since 0.0.1 */ public static DefaultFormatter createRegexFormatter(final Pattern regex) { if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(regex)); if (null == regex) { throw new RuntimeExceptionIsNull("regex"); //$NON-NLS-1$ } final DefaultFormatter result = new DefaultFormatter() { private static final long serialVersionUID = 2665033244806980400L; final transient Matcher matcher; { setCommitsOnValidEdit(true); setOverwriteMode(false); matcher = regex.matcher(HelperString.EMPTY_STRING); } @Override public Object stringToValue(final String string) throws ParseException { if (null == string) { return null; } matcher.reset(string); if (!matcher.matches()) { throw new ParseException("does not match regex", 0); //$NON-NLS-1$ } return super.stringToValue(string); } }; if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result)); return result; }
private static void testDefaultFormatter() { testDefaultFormatter(new DefaultFormatter() { }); testDefaultFormatter(new DefaultFormatter()); }
public static DefaultFormatter getSpinnerFormatter(JSpinner spinner) { JComponent comp = spinner.getEditor(); JFormattedTextField field = (JFormattedTextField)comp.getComponent(0); return (DefaultFormatter)field.getFormatter(); }
private void checkDefaultFormatter(final DefaultFormatterFactory factory) { assertTrue(factory.getDefaultFormatter() instanceof DefaultFormatter); assertNull(factory.getDisplayFormatter()); assertNull(factory.getEditFormatter()); assertNull(factory.getNullFormatter()); }
private void initDialog() { dialog = new JDialog(mainWindow.getFrame(), true); dialog.setLocationRelativeTo(mainWindow.getFrame()); dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); Container panel = dialog.getContentPane(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); JPanel infoPanel = new JPanel(); infoPanel.setLayout(new FormLayout(new ColumnSpec[] { FormSpecs.RELATED_GAP_COLSPEC, FormSpecs.DEFAULT_COLSPEC, FormSpecs.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"), FormSpecs.RELATED_GAP_COLSPEC, }, new RowSpec[] { FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, })); JLabel frequenciaLabel = new JLabel("Frequência:"); infoPanel.add(frequenciaLabel, "2, 2, left, center"); frequenciaComboBox = new JComboBox<Frequencia>(); frequenciaComboBox.setModel(new DefaultComboBoxModel<>(TopMusicalConfiguration.FREQUENCIAS_PERMITIDAS)); infoPanel.add(frequenciaComboBox, "4, 2, fill, default"); JLabel quantidadePosicoesLabel = new JLabel("Quantidade de Posições:"); infoPanel.add(quantidadePosicoesLabel, "2, 4, left, center"); quantidadePosicoesSpinner = new JSpinner(); JComponent comp = quantidadePosicoesSpinner.getEditor(); JFormattedTextField field = (JFormattedTextField) comp.getComponent(0); DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); formatter.setCommitsOnValidEdit(true); infoPanel.add(quantidadePosicoesSpinner, "4, 4"); JPanel botoesPanel = new JPanel(); salvarButton = ButtonFactory.createJButton("Salvar", "Salva as configurações."); botoesPanel.add(salvarButton); cancelarButton = ButtonFactory.createJButton("Cancelar", "Cancela as alterações feitas"); botoesPanel.add(cancelarButton); panel.add(infoPanel); panel.add(botoesPanel); dialog.pack(); }
public void configure(JFormattedTextField textField, DefaultFormatter formatter) { textField.setFocusLostBehavior(JFormattedTextField.PERSIST); formatter.setOverwriteMode(false); formatter.setAllowsInvalid(true); formatter.setCommitsOnValidEdit(true); }
public void configure(JFormattedTextField textField, DefaultFormatter formatter) { textField.setFocusLostBehavior(JFormattedTextField.COMMIT); formatter.setOverwriteMode(false); formatter.setAllowsInvalid(true); formatter.setCommitsOnValidEdit(false); }
public void configure(JFormattedTextField textField, DefaultFormatter formatter) { textField.setFocusLostBehavior(JFormattedTextField.PERSIST); formatter.setOverwriteMode(false); formatter.setAllowsInvalid(true); formatter.setCommitsOnValidEdit(false); }
private static void spinnerSetCommitOnEdit(JSpinner spinner) { JComponent comp = spinner.getEditor(); JFormattedTextField field = (JFormattedTextField) comp.getComponent(0); DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); formatter.setCommitsOnValidEdit(true); }
/** * Initializes the graphical user interface for this search implementation. * Consists of a textfield for the search term and a checkbox, that is used * to hide all nodes that don't match a search string. */ private void initGUI() { mSearchInputPanel = new JPanel(); mSearchInputPanel.setLayout(new BoxLayout(mSearchInputPanel, BoxLayout.X_AXIS)); DefaultFormatter formatter = new DefaultFormatter(); formatter.setOverwriteMode(false); formatter.setCommitsOnValidEdit(true); mInputTextfield = new JFormattedTextField(formatter); mInputTextfield.setToolTipText("<html>Search terms separator: <b>" + SEARCH_TERM_DELIMITER + "</b><br>" + "Inner search term separator: <b>" + SUB_SEARCH_TERM_DELIMITER + "</b><br>" + "Exact match enclosing character: <b>" + ENCLOSING_CHAR + "</b><br>" + "Example: ip,10.0.0;mac,\"aa:bb:cc:dd:ee:ff\"<br>" + " detects and highlights all nodes that <b>contain</b> the strings " + "<i>ip</i> and <i>10.0.0</i>,<br>" + "as well as all nodes that <b>contain</b> the string mac and <b>exactly contain</b> " + "the value <i>aa:bb:cc:dd:ee:ff</i>" + "</html>"); mInputTextfield.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { mSearchableGraphPanel.search(mInputTextfield.getText()); } }); mHideSearchMismatchCheckbox = new JCheckBox(); mHideSearchMismatchCheckbox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { mSearchableGraphPanel .setHideSearchMismatches(mHideSearchMismatchCheckbox .isSelected()); } }); mSearchInputPanel.add(new JLabel("Search: ")); mSearchInputPanel.add(mInputTextfield); mSearchInputPanel.add(new JLabel("Hide search mismatches: ")); mSearchInputPanel.add(mHideSearchMismatchCheckbox); }
public abstract void configure(JFormattedTextField textField, DefaultFormatter formatter);