Example usage for javax.swing.text NumberFormatter setAllowsInvalid

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

Introduction

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

Prototype

public void setAllowsInvalid(boolean allowsInvalid) 

Source Link

Document

Sets whether or not the value being edited is allowed to be invalid for a length of time (that is, stringToValue throws a ParseException).

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(2);//from   w w w. j a  v  a2  s.c  om
    format.setMinimumFractionDigits(2);
    format.setParseIntegerOnly(true);
    format.setRoundingMode(RoundingMode.HALF_UP);

    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMaximum(1000);
    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);
    tf2.setValue(1111.1111);
    JFormattedTextField tf3 = new JFormattedTextField(formatter);
    tf3.setValue(-1111.1111);
    JFormattedTextField tf4 = 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);
}

From source file:components.ConversionPanel.java

ConversionPanel(Converter myController, String myTitle, Unit[] myUnits, ConverterRangeModel myModel) {
    if (MULTICOLORED) {
        setOpaque(true);/*w  ww .  j  a  v  a  2 s.  c o  m*/
        setBackground(new Color(0, 255, 255));
    }
    setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(myTitle),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Save arguments in instance variables.
    controller = myController;
    units = myUnits;
    title = myTitle;
    sliderModel = myModel;

    //Create the text field format, and then the text field.
    numberFormat = NumberFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(2);
    NumberFormatter formatter = new NumberFormatter(numberFormat);
    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
    //be parsed (that happens on focus loss/return, I think).
    //
    textField = new JFormattedTextField(formatter);
    textField.setColumns(10);
    textField.setValue(new Double(sliderModel.getDoubleValue()));
    textField.addPropertyChangeListener(this);

    //Add the combo box.
    unitChooser = new JComboBox();
    for (int i = 0; i < units.length; i++) { //Populate it.
        unitChooser.addItem(units[i].description);
    }
    unitChooser.setSelectedIndex(0);
    sliderModel.setMultiplier(units[0].multiplier);
    unitChooser.addActionListener(this);

    //Add the slider.
    slider = new JSlider(sliderModel);
    sliderModel.addChangeListener(this);

    //Make the text field/slider group a fixed size
    //to make stacked ConversionPanels nicely aligned.
    JPanel unitGroup = new JPanel() {
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        public Dimension getPreferredSize() {
            return new Dimension(150, super.getPreferredSize().height);
        }

        public Dimension getMaximumSize() {
            return getPreferredSize();
        }
    };
    unitGroup.setLayout(new BoxLayout(unitGroup, BoxLayout.PAGE_AXIS));
    if (MULTICOLORED) {
        unitGroup.setOpaque(true);
        unitGroup.setBackground(new Color(0, 0, 255));
    }
    unitGroup.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
    unitGroup.add(textField);
    unitGroup.add(slider);

    //Create a subpanel so the combo box isn't too tall
    //and is sufficiently wide.
    JPanel chooserPanel = new JPanel();
    chooserPanel.setLayout(new BoxLayout(chooserPanel, BoxLayout.PAGE_AXIS));
    if (MULTICOLORED) {
        chooserPanel.setOpaque(true);
        chooserPanel.setBackground(new Color(255, 0, 255));
    }
    chooserPanel.add(unitChooser);
    chooserPanel.add(Box.createHorizontalStrut(100));

    //Put everything together.
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    add(unitGroup);
    add(chooserPanel);
    unitGroup.setAlignmentY(TOP_ALIGNMENT);
    chooserPanel.setAlignmentY(TOP_ALIGNMENT);
}

From source file:Converter.java

ConversionPanel(Converter myController, String myTitle, Unit[] myUnits, ConverterRangeModel myModel) {
    if (MULTICOLORED) {
        setOpaque(true);/*w w w  .  j a  v  a2  s.  c  om*/
        setBackground(new Color(0, 255, 255));
    }
    setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(myTitle),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    // Save arguments in instance variables.
    controller = myController;
    units = myUnits;
    title = myTitle;
    sliderModel = myModel;

    // Create the text field format, and then the text field.
    numberFormat = NumberFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(2);
    NumberFormatter formatter = new NumberFormatter(numberFormat);
    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
    // be parsed (that happens on focus loss/return, I think).
    //
    textField = new JFormattedTextField(formatter);
    textField.setColumns(10);
    textField.setValue(new Double(sliderModel.getDoubleValue()));
    textField.addPropertyChangeListener(this);

    // Add the combo box.
    unitChooser = new JComboBox();
    for (int i = 0; i < units.length; i++) { // Populate it.
        unitChooser.addItem(units[i].description);
    }
    unitChooser.setSelectedIndex(0);
    sliderModel.setMultiplier(units[0].multiplier);
    unitChooser.addActionListener(this);

    // Add the slider.
    slider = new JSlider(sliderModel);
    sliderModel.addChangeListener(this);

    // Make the text field/slider group a fixed size
    // to make stacked ConversionPanels nicely aligned.
    JPanel unitGroup = new JPanel() {
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        public Dimension getPreferredSize() {
            return new Dimension(150, super.getPreferredSize().height);
        }

        public Dimension getMaximumSize() {
            return getPreferredSize();
        }
    };
    unitGroup.setLayout(new BoxLayout(unitGroup, BoxLayout.PAGE_AXIS));
    if (MULTICOLORED) {
        unitGroup.setOpaque(true);
        unitGroup.setBackground(new Color(0, 0, 255));
    }
    unitGroup.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
    unitGroup.add(textField);
    unitGroup.add(slider);

    // Create a subpanel so the combo box isn't too tall
    // and is sufficiently wide.
    JPanel chooserPanel = new JPanel();
    chooserPanel.setLayout(new BoxLayout(chooserPanel, BoxLayout.PAGE_AXIS));
    if (MULTICOLORED) {
        chooserPanel.setOpaque(true);
        chooserPanel.setBackground(new Color(255, 0, 255));
    }
    chooserPanel.add(unitChooser);
    chooserPanel.add(Box.createHorizontalStrut(100));

    // Put everything together.
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    add(unitGroup);
    add(chooserPanel);
    unitGroup.setAlignmentY(TOP_ALIGNMENT);
    chooserPanel.setAlignmentY(TOP_ALIGNMENT);
}

From source file:dbseer.gui.panel.DBSeerMiddlewarePanel.java

private void initializeGUI() {
    this.setLayout(new MigLayout());

    JLabel ipAddressLabel = new JLabel("IP Address:");
    JLabel portLabel = new JLabel("Port:");
    JLabel idLabel = new JLabel("ID:");
    JLabel passwordLabel = new JLabel("Password:");

    ipField = new JTextField(20);

    DecimalFormat portFormatter = new DecimalFormat();

    portFormatter.setMaximumFractionDigits(0);
    portFormatter.setMaximumIntegerDigits(5);
    portFormatter.setMinimumIntegerDigits(1);
    portFormatter.setDecimalSeparatorAlwaysShown(false);
    portFormatter.setGroupingUsed(false);

    portField = new JFormattedTextField(portFormatter);
    portField.setColumns(6);// w ww .  jav a2 s .co  m
    portField.setText("3555"); // default port.
    idField = new JTextField(20);
    passwordField = new JPasswordField(20);

    logInOutButton = new JButton("Login");
    logInOutButton.addActionListener(this);
    startMonitoringButton = new JButton("Start Monitoring");
    startMonitoringButton.addActionListener(this);
    stopMonitoringButton = new JButton("Stop Monitoring");
    stopMonitoringButton.addActionListener(this);

    startMonitoringButton.setEnabled(true);
    stopMonitoringButton.setEnabled(false);

    ipField.setText(DBSeerGUI.userSettings.getLastMiddlewareIP());
    portField.setText(String.valueOf(DBSeerGUI.userSettings.getLastMiddlewarePort()));
    idField.setText(DBSeerGUI.userSettings.getLastMiddlewareID());

    NumberFormatter formatter = new NumberFormatter(NumberFormat.getIntegerInstance());
    formatter.setMinimum(1);
    formatter.setMaximum(120);
    formatter.setAllowsInvalid(false);

    refreshRateLabel = new JLabel("Monitoring Refresh Rate:");
    refreshRateField = new JFormattedTextField(formatter);
    JLabel refreshRateRangeLabel = new JLabel("(1~120 sec)");

    refreshRateField.setText("1");
    applyRefreshRateButton = new JButton("Apply");
    applyRefreshRateButton.addActionListener(this);

    this.add(ipAddressLabel, "cell 0 0 2 1, split 4");
    this.add(ipField);
    this.add(portLabel);
    this.add(portField);
    this.add(idLabel, "cell 0 2");
    this.add(idField, "cell 1 2");
    this.add(passwordLabel, "cell 0 3");
    this.add(passwordField, "cell 1 3");
    this.add(refreshRateLabel, "cell 0 4");
    this.add(refreshRateField, "cell 1 4, growx, split 3");
    this.add(refreshRateRangeLabel);
    this.add(applyRefreshRateButton, "growx, wrap");
    //      this.add(logInOutButton, "cell 0 2 2 1, growx, split 3");
    this.add(startMonitoringButton);
    this.add(stopMonitoringButton);
}