Refine search
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); } }
/** * Configures the behavior when inserting characters. If <code>overwriteMode</code> is true (the default), new * characters overwrite existing characters in the model. * * @param overwriteMode Indicates if overwrite or overstrike mode is used */ public void setOverwriteMode(boolean overwriteMode) { _formatter.setOverwriteMode(overwriteMode); }
public Object stringToValue(String text) throws ParseException { Pattern pattern = getPattern(); if (pattern != null && !TextUtils.isEmpty(text)) { Matcher matcher = pattern.matcher(text); if (matcher.matches()) { return super.stringToValue(text); } throw new ParseException("Pattern did not match", 0); } return text; } }
formatter.setMaximum(1000.0); formatter.setMinimum(0.0); formatter.setAllowsInvalid(false); formatter.setOverwriteMode(false); JFormattedTextField tf = new JFormattedTextField(formatter); tf.setColumns(10); tf.setValue(123456789.99); JFormattedTextField tf1 = new JFormattedTextField(formatter); tf1.setValue(1234567890.99); JFormattedTextField tf2 = new JFormattedTextField(formatter); tf4.setValue(-56); JFrame frame = new JFrame("Test"); frame.setLayout(new GridLayout(5, 0)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tf); frame.add(tf1); frame.add(tf2); frame.add(tf3); frame.add(tf4); frame.pack(); frame.setVisible(true);
setMask("##/##/####"); setPlaceholderCharacter('0'); setAllowsInvalid(false); setOverwriteMode(true); } catch (ParseException e) { e.printStackTrace(); final MaskFormatter formatter = new TimeFormatter(); // textfield 1: create formatter and textfield final JFormattedTextField tf2 = new JFormattedTextField(formatter);// textfield 2: create formatter and textfield tf2.setValue(new Date()); // no initial value final JLabel label = new JLabel(); JButton bt = new JButton("Show Value");// button to show current value @Override public void actionPerformed(ActionEvent e) { System.out.println(" value 2 = " + tf2.getValue()); System.out.println(" value 2 = " + tf2.getText()); System.out.println("value class: " + formatter.getValueClass()); label.setText(tf2.getText());
JFrame f = new JFrame("Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFormattedTextField tf = new JFormattedTextField("Xxxx yyy zzzz."); DefaultFormatter df = (DefaultFormatter) tf.getFormatter(); df.setOverwriteMode(true); f.add(tf); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS)); if (MULTICOLORED) { mainPane.setOpaque(true); mainPane.add(Box.createRigidArea(new Dimension(0, 5))); mainPane.add(metricPanel); mainPane.add(Box.createRigidArea(new Dimension(0, 5))); mainPane.add(usaPanel); formatter.setAllowsInvalid(false); formatter.setCommitsOnValidEdit(true);// seems to be a no-op -- aha -- it changes the value property but doesn't cause the result to textField = new JFormattedTextField(formatter); textField.setColumns(10); textField.setValue(new Double(sliderModel.getDoubleValue())); textField.addPropertyChangeListener(this); unitChooser = new JComboBox(); // Add the combo box. int max = sliderModel.getMaximum(); double value = sliderModel.getDoubleValue(); NumberFormatter formatter = (NumberFormatter) textField.getFormatter(); formatter.setMinimum(new Double(min)); formatter.setMaximum(new Double(max));
DateFormatter formatter = (DateFormatter)editor.getTextField().getFormatter(); formatter.setAllowsInvalid(false); // this makes what you want formatter.setOverwriteMode(true); content.add(spinner); JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.getContentPane().add(content); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
formatter.setMinimum(1); formatter.setMaximum(100); formatter.setAllowsInvalid(false); formatter.setOverwriteMode(true); JFormattedTextField textField = new JFormattedTextField(formatter); textField.setColumns(5); textField.setValue(1); gridPanel.add(textField); public void actionPerformed(ActionEvent e) { List list = new ArrayList(); for(Component comp : gridPanel.getComponents()) { JFormattedTextField textField = (JFormattedTextField)comp; Integer value = (Integer)textField.getValue(); Color foreground = list.contains(value) ? Color.RED : Color.BLACK; textField.setForeground(foreground); JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.add(gridPanel, BorderLayout.CENTER); frame.add(button, BorderLayout.SOUTH); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
JFrame f = new JFrame("Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new GridLayout(0, 1)); JFormattedTextField ccyTxt = new JFormattedTextField(); DecimalFormat ccyTxtFormat = (DecimalFormat) DecimalFormat.getNumberInstance(); ccyTxtFormat.setMaximumIntegerDigits((int) 13); ccyTxtFormat.setMinimumFractionDigits(0); NumberFormatter ccyTxtFormatter = new NumberFormatter(ccyTxtFormat); ccyTxtFormatter.setAllowsInvalid(true); DefaultFormatterFactory CcyTxtDecimalFormatterFactory = new DefaultFormatterFactory(ccyTxtFormatter); ccyTxt.setFormatterFactory(CcyTxtDecimalFormatterFactory); ccyTxt.setColumns(12); f.add(ccyTxt); f.add(new JButton("Button")); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
JFrame f = new JFrame("JSpinnerTest"); f.add(new JSpinnerTest()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); spinner.setEditor(editor); JFormattedTextField jtf = editor.getTextField(); DefaultFormatter formatter = (DefaultFormatter) jtf.getFormatter(); formatter.setCommitsOnValidEdit(true); spinner.addChangeListener(new ChangeListener() { this.add(spinner); this.add(label);
cipherGUIFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cipherGUIFrame.pack(); cipherGUIFrame.setLocationRelativeTo(null); cipherGUIFrame.setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 600); JPanel mainframe = new JPanel(); JToggleButton mybutton = new JToggleButton("ENCRYPT"); mainframe.setLayout(new BoxLayout(mainframe, BoxLayout.Y_AXIS)); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS)); p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); p1.setBorder(BorderFactory.createTitledBorder("Cleartext")); p2.setBorder(BorderFactory.createTitledBorder("KEY")); JFormattedTextField field = (JFormattedTextField) comp.getComponent(0); DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); formatter.setCommitsOnValidEdit(true); myspinner.addChangeListener(new ChangeListener() {
this.setSize(400,400); container = new JPanel(); btnProfit = new JButton("Calculate"); buyPrice = new JFormattedTextField(getIntFormatter(NUMBER_FORMAT)); sellPrice = new JFormattedTextField(getIntFormatter(NUMBER_FORMAT)); resultField = new JTextField(); buyLabel = new JLabel("The price you intend to pay"); btnProfit.addActionListener(this); GridLayout gridLayout = new GridLayout(3,2); container.setLayout(gridLayout); container.add(buyLabel); container.add(sellLabel); container.add(buyPrice); container.add(sellPrice); this.add(container); this.pack(); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); formatter.setValueClass(Integer.class); formatter.setMinimum(0); formatter.setMaximum(Integer.MAX_VALUE); formatter.setCommitsOnValidEdit(true);
this.jSpinner2.setModel(model2); (((JSpinner.NumberEditor)this.jSpinner1.getEditor()).getTextField()).setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() { @Override public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) { format.setMinimumFractionDigits(1); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); formatter.setMinimum(-1000.0); formatter.setMaximum(1000.00); format.setMinimumFractionDigits(1); InternationalFormatter formatter = new InternationalFormatter(format); formatter.setAllowsInvalid(false); formatter.setMinimum(-1000.0); formatter.setMaximum(1000.00); jSpinner2 = new javax.swing.JSpinner(); jSpinner2.setRequestFocusEnabled(false); ); pack(); new NewJFrame().setVisible(true);
add(mySpinner); pack(); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); DateFormatter formatter = new DateFormatter(new SimpleDateFormat(pattern)); formatter.setAllowsInvalid(false); formatter.setOverwriteMode(true); setFormatterFactory(new DefaultFormatterFactory(formatter)); updateValue(); spinner.addChangeListener(this); setValue((Date) spinner.getModel().getValue());
value.setEditor(new JSpinner.NumberEditor(value,"#")); //prevent digit grouping, e.g. 1,000 jtf = ((JSpinner.DefaultEditor) value.getEditor()).getTextField(); DefaultFormatter df = (DefaultFormatter)jtf.getFormatter(); df.setCommitsOnValidEdit(true); getContentPane().setLayout(new BorderLayout()); getContentPane().add(value,BorderLayout.CENTER); getContentPane().add(pnlButton, BorderLayout.PAGE_END); pack();
setValues(); JPanel panel = new JPanel(); panel.add(new JLabel("price:")); panel.add(price); formatter.setMinimum(0.0); formatter.setMaximum(10000000.0); formatter.setAllowsInvalid(false); price = new JFormattedTextField(formatter); price.setColumns(10); price.setValue(0.0);
super(new JFormattedTextField()); textField = (JFormattedTextField) getComponent(); formatter.setOverwriteMode(false); textField.setFormatterFactory(new DefaultFormatterFactory(formatter)); textField.setHorizontalAlignment(JTextField.TRAILING); textField.setFocusLostBehavior(JFormattedTextField.PERSIST);
final DefaultFormatter formatter = new DefaultFormatter(); formatter.setOverwriteMode(false); textField = new JFormattedTextField(formatter); textField.setValue(s); textField.setPreferredSize(GUIDefaults.TEXT_BOX_DIMENSION); panel = new JPanel(new BorderLayout(GUIDefaults.hGap, GUIDefaults.vGap)); final JLabel label = new JLabel(getDescription());
private void initTexField(final JFormattedTextField field, Number value) { field.setColumns(10); field.setValue(toDouble(value) / palette.getScale()); if (readOnly) { field.setEnabled(false); } else { //don't automatically revert invalid values (show msg instead): field.setFocusLostBehavior(JFormattedTextField.COMMIT); //post all valid edits: allows for applying all validations while still typing instead of on focus loss ((DefaultFormatter)field.getFormatter()).setCommitsOnValidEdit(true); //caret fix: default behavior puts it before 1st char, no matter where you click field.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent e) { final int dot = field.getCaret().getDot(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { field.getCaret().setDot(dot); } }); } }); } }