Example usage for javax.swing.text NumberFormatter setValueClass

List of usage examples for javax.swing.text NumberFormatter setValueClass

Introduction

In this page you can find the example usage for javax.swing.text NumberFormatter setValueClass.

Prototype

public void setValueClass(Class<?> valueClass) 

Source Link

Document

Sets that class that is used to create new Objects.

Usage

From source file:com.jgoodies.validation.tutorial.formatted.NumberExample.java

/**
 * Appends the demo rows to the given builder and returns the List of
 * formatted text fields.//from   w w  w .j  a  v a2  s.c o  m
 * 
 * @param builder  the builder used to add components to
 * @return the List of formatted text fields
 */
private List appendDemoRows(DefaultFormBuilder builder) {
    // The Formatter is choosen by the initial value.
    JFormattedTextField defaultNumberField = new JFormattedTextField(new Long(42));

    // The Formatter is choosen by the given Format.
    JFormattedTextField noInitialValueField = new JFormattedTextField(NumberFormat.getIntegerInstance());

    // Uses a custom NumberFormat.
    NumberFormat customFormat = NumberFormat.getIntegerInstance();
    customFormat.setMinimumIntegerDigits(3);
    JFormattedTextField customFormatField = new JFormattedTextField(new NumberFormatter(customFormat));

    // Uses a custom NumberFormatter that prints natural language strings.
    JFormattedTextField customFormatterField = new JFormattedTextField(new CustomNumberFormatter());

    // Uses a custom FormatterFactory that used different formatters
    // for the display and while editing.
    DefaultFormatterFactory formatterFactory = new DefaultFormatterFactory(new NumberFormatter(),
            new CustomNumberFormatter());
    JFormattedTextField formatterFactoryField = new JFormattedTextField(formatterFactory);

    // Wraps a NumberFormatter to map empty strings to null and vice versa.
    JFormattedTextField numberOrNullField = new JFormattedTextField(new EmptyNumberFormatter());

    // Wraps a NumberFormatter to map empty strings to -1 and vice versa.
    Integer emptyValue = new Integer(-1);
    JFormattedTextField numberOrEmptyValueField = new JFormattedTextField(new EmptyNumberFormatter(emptyValue));
    numberOrEmptyValueField.setValue(emptyValue);

    // Commits values on valid edit texts.
    DefaultFormatter formatter = new NumberFormatter();
    formatter.setCommitsOnValidEdit(true);
    JFormattedTextField commitOnValidEditField = new JFormattedTextField(formatter);

    // Returns number values of type Integer
    NumberFormatter numberFormatter = new NumberFormatter();
    numberFormatter.setValueClass(Integer.class);
    JFormattedTextField integerField = new JFormattedTextField(numberFormatter);

    Format displayFormat = new DisplayFormat(NumberFormat.getIntegerInstance());
    Format typedDisplayFormat = new DisplayFormat(NumberFormat.getIntegerInstance(), true);
    List fields = new LinkedList();
    fields.add(Utils.appendRow(builder, "Default", defaultNumberField, typedDisplayFormat));
    fields.add(Utils.appendRow(builder, "No initial value", noInitialValueField, displayFormat));
    fields.add(Utils.appendRow(builder, "Empty <-> null", numberOrNullField, displayFormat));
    fields.add(Utils.appendRow(builder, "Empty <->   -1", numberOrEmptyValueField, displayFormat));
    fields.add(Utils.appendRow(builder, "Custom format", customFormatField, displayFormat));
    fields.add(Utils.appendRow(builder, "Custom formatter", customFormatterField, displayFormat));
    fields.add(Utils.appendRow(builder, "Formatter factory", formatterFactoryField, displayFormat));
    fields.add(Utils.appendRow(builder, "Commits on valid edit", commitOnValidEditField, displayFormat));
    fields.add(Utils.appendRow(builder, "Integer Result", integerField, typedDisplayFormat));

    return fields;
}

From source file:org.photovault.swingui.PhotoInfoEditor.java

protected void createTechDataUI() {
    JPanel pane = new JPanel();
    tabPane.addTab("Tech data", pane);

    JLabel cameraMakerLabel = new JLabel("Camera make");
    JTextField cameraMakerField = createMvTextField("cameraMaker", 20);
    // cameraDoc = cameraField.getDocument();

    JLabel cameraLabel = new JLabel("Camera model");
    cameraField = createMvTextField("camera", 20);
    cameraDoc = cameraField.getDocument();

    JLabel lensLabel = new JLabel("Lens");
    lensField = createMvTextField("lens", 20);
    lensDoc = lensField.getDocument();/*www  .j  a v  a  2s.  co  m*/

    JLabel filmLabel = new JLabel("Film");
    filmField = createMvTextField("film", 20);
    filmDoc = filmField.getDocument();

    JLabel filmSpeedLabel = new JLabel("Film speed");
    NumberFormatter filmSpeedFormatter = new NumberFormatter(new DecimalFormat("#########0"));
    filmSpeedFormatter.setValueClass(PhotoInfoFields.FILM_SPEED.getType());
    filmSpeedField = new JFormattedTextField(filmSpeedFormatter);

    filmSpeedField.setColumns(5);
    filmSpeedField.addPropertyChangeListener(this);
    filmSpeedField.putClientProperty(FIELD, PhotoInfoFields.FILM_SPEED);

    JLabel shutterSpeedLabel = new JLabel("Shutter speed");
    DecimalFormat shutterSpeedFormat = new DecimalFormat("###0.####");
    NumberFormatter shutterSpeedFormatter = new NumberFormatter(shutterSpeedFormat);
    shutterSpeedFormatter.setValueClass(PhotoInfoFields.SHUTTER_SPEED.getType());
    shutterSpeedField = new JFormattedTextField(shutterSpeedFormatter);
    shutterSpeedField.setColumns(5);
    shutterSpeedField.addPropertyChangeListener(this);
    shutterSpeedField.putClientProperty(FIELD, PhotoInfoFields.SHUTTER_SPEED);

    JLabel fStopLabel = new JLabel("F-stop");
    DecimalFormat fStopFormat = new DecimalFormat("#0.#");
    NumberFormatter fStopFormatter = new NumberFormatter(fStopFormat);
    fStopFormatter.setValueClass(PhotoInfoFields.FSTOP.getType());
    fStopField = new JFormattedTextField(fStopFormatter);
    fStopField.setColumns(5);
    fStopField.addPropertyChangeListener(this);
    fStopField.putClientProperty(FIELD, PhotoInfoFields.FSTOP);

    JLabel focalLengthLabel = new JLabel("Focal length");
    NumberFormatter focalLengthFormatter = new NumberFormatter(new DecimalFormat("#######0.#"));
    focalLengthFormatter.setValueClass(PhotoInfoFields.FSTOP.getType());
    focalLengthField = new JFormattedTextField(focalLengthFormatter);
    focalLengthField.setColumns(5);
    focalLengthField.addPropertyChangeListener(this);
    focalLengthField.putClientProperty(FIELD, PhotoInfoFields.FOCAL_LENGTH);

    // Tech note text
    JLabel notesLabel = new JLabel("Tech. notes");
    technoteTextArea = new JTextArea(5, 40);
    technoteTextArea.setLineWrap(true);
    technoteTextArea.setWrapStyleWord(true);
    JScrollPane technoteScrollPane = new JScrollPane(technoteTextArea);
    technoteScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    Border technoteBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
    technoteBorder = BorderFactory.createTitledBorder(technoteBorder, "Description");
    technoteScrollPane.setBorder(technoteBorder);
    technoteDoc = technoteTextArea.getDocument();
    technoteDoc.putProperty(FIELD, PhotoInfoFields.TECH_NOTES);
    technoteDoc.addDocumentListener(this);

    // Lay out the created controls
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    pane.setLayout(layout);
    JLabel[] labels = { cameraMakerLabel, cameraLabel, lensLabel, focalLengthLabel, filmLabel, filmSpeedLabel,
            shutterSpeedLabel, fStopLabel };
    JTextField[] fields = { cameraMakerField, cameraField, lensField, focalLengthField, filmField,
            filmSpeedField, shutterSpeedField, fStopField };
    addLabelTextRows(labels, fields, layout, pane);
    pane.add(technoteScrollPane);
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weighty = 0.5;
    c.fill = GridBagConstraints.BOTH;
    layout.setConstraints(technoteScrollPane, c);
}

From source file:org.yccheok.jstock.gui.NewBuyTransactionJDialog.java

private JFormattedTextField getCurrencyJFormattedTextField() {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(4);//from ww w .  j  av  a  2 s. c  o m
    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMinimum(0.0);
    formatter.setValueClass(Double.class);
    JFormattedTextField formattedTextField = new JFormattedTextField(formatter);
    formattedTextField.addMouseListener(getJFormattedTextFieldMouseListener());
    return formattedTextField;
}

From source file:org.yccheok.jstock.gui.NewSellTransactionJDialog.java

private JFormattedTextField getPercentageJFormattedTextField() {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(2);//  w  w w. j  a va  2  s  .  com
    format.setMinimumFractionDigits(2);
    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setValueClass(Double.class);
    JFormattedTextField formattedTextField = new JFormattedTextField(formatter);
    return formattedTextField;
}

From source file:org.yccheok.jstock.gui.NewSellTransactionJDialog.java

private JFormattedTextField getCurrencyJFormattedTextField(boolean isNegativeAllowed) {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(4);//  w  ww . j ava  2s .com
    NumberFormatter formatter = new NumberFormatter(format);

    if (isNegativeAllowed == false)
        formatter.setMinimum(0.0);
    else
        formatter.setMinimum(null);

    formatter.setValueClass(Double.class);
    JFormattedTextField formattedTextField = new JFormattedTextField(formatter);
    formattedTextField.addMouseListener(getJFormattedTextFieldMouseListener());
    return formattedTextField;
}

From source file:org.yccheok.jstock.gui.OptionsNetworkJPanel.java

private JFormattedTextField getPortNumberJFormattedTextField() {
    DecimalFormat df = new DecimalFormat("#####");
    NumberFormatter nf = new NumberFormatter(df) {
        @Override//from w ww .  ja  v  a2s  .c o  m
        public String valueToString(Object iv) throws ParseException {
            if ((iv == null) || (((Integer) iv).intValue() == -1)) {
                return "";
            } else {
                return super.valueToString(iv);
            }
        }

        @Override
        public Object stringToValue(String text) throws ParseException {
            if ("".equals(text)) {
                return null;
            }
            return super.stringToValue(text);
        }
    };
    nf.setMinimum(0);
    nf.setMaximum(65534);
    nf.setValueClass(Integer.class);
    return new JFormattedTextField(nf);
}

From source file:org.yccheok.jstock.gui.OptionsSellAdvisorJPanel.java

private JFormattedTextField getPercentageJFormattedTextField() {
    NumberFormat format = NumberFormat.getNumberInstance();
    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMinimum(0.0);/*from   ww  w . j ava 2 s. c  o m*/
    formatter.setMaximum(null);
    formatter.setValueClass(Double.class);
    JFormattedTextField field = new JFormattedTextField(formatter);

    return field;
}

From source file:org.yccheok.jstock.gui.portfolio.AutoDividendJDialog.java

private JFormattedTextField getCurrencyJFormattedTextField() {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(3);/* w  w  w . j av  a 2  s. c om*/
    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMinimum(0.0);
    formatter.setValueClass(Double.class);
    JFormattedTextField formattedTextField = new JFormattedTextField(formatter);
    formattedTextField.addMouseListener(getJFormattedTextFieldMouseListener());
    return formattedTextField;
}

From source file:org.yccheok.jstock.gui.portfolio.SplitJDialog.java

private JFormattedTextField getUnitJFormattedTextField() {
    NumberFormat format = new DecimalFormat("#,##0.###");
    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMinimum(1.0);/* w w w. java  2  s  .  com*/
    formatter.setValueClass(Double.class);
    JFormattedTextField field = new JFormattedTextField(formatter);
    return field;
}