Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013-2014 Erwin Müller <erwin.mueller@deventm.org>
   *
   * This file is part of prefdialog-misc-swing.
   *
   * prefdialog-misc-swing is free software: you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as published by the
   * Free Software Foundation, either version 3 of the License, or (at your
   * option) any later version.
  *
  * prefdialog-misc-swing is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  * details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with prefdialog-misc-swing. If not, see <http://www.gnu.org/licenses/>.
  */
 package com.anrisoftware.prefdialog.miscswing.validatingfields;
 
 import static javax.swing.SwingUtilities.invokeLater;
 
 import java.awt.Color;
 
 
 
Using an input verifier to verify the input on focus lost and input enter. Mark the field and show a tool-tip text if the input is not valid.

Author(s):
Erwin Mueller, erwin.mueller@deventm.org
Since:
3.0
 
 @SuppressWarnings("serial")
 
     private InputVerifier verifier;
 
     private ActionListener validateAction;
 
     private InputVerifier parentVerifier;
 
     private ValidatingComponent validating;

    

See also:
JFormattedTextField.javax.swing.JFormattedTextField.()
 
     public ValidatingFormattedTextField() {
         super();
         setupField();
     }

    
 
     public ValidatingFormattedTextField(AbstractFormatter formatter) {
         super(formatter);
         setupField();
     }

    
 
             Object currentValue) {
         super(factorycurrentValue);
         setupField();
     }

    
 
         super(factory);
         setupField();
     }

    
 
     public ValidatingFormattedTextField(Format format) {
         super(format);
         setupField();
     }

    
    public ValidatingFormattedTextField(Object value) {
        super(value);
        setupField();
    }
    private void setupField() {
        this. = new ValidatingComponent(thisnew ToolTipShower(this));
        this. = new InputVerifier() {
            @Override
            public boolean verify(JComponent input) {
                return .isValid();
            }
        };
        this. = new InputVerifier() {
            @Override
            public boolean verify(JComponent input) {
                JFormattedTextField field = ValidatingFormattedTextField.this;
                boolean valid = verifyField(field);
                setValidInAWT(valid);
                return valid;
            }
        };
        this. = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                verifyField();
            }
        };
        setInputVerifier();
    }

    
Verify the field from an external event.
    protected void verifyField() {
        .shouldYieldFocus(this);
    }

    
Verifies the field.

Parameters:
verifier the javax.swing.InputVerifier that verifiers the input.
input the JTextFormattedField.
Returns:
true if the input is valid and the focus can be yield.
    protected boolean verifyField(InputVerifier verifier,
            JFormattedTextField input) {
        try {
            input.commitEdit();
            return verifier.verify(input);
        } catch (ParseException e) {
            return false;
        }
    }
    private void setValidInAWT(final boolean valid) {
        invokeLater(new Runnable() {
            @Override
            public void run() {
                setInputValid(valid);
            }
        });
    }

    
Sets the input verifier that validated the input.

Parameters:
verifier the javax.swing.InputVerifier.
    public void setVerifier(InputVerifier verifier) {
        this. = verifier;
    }

    
Returns the input verifier that validated the input.

    public InputVerifier getVerifier() {
        return ;
    }

    
    public void setInvalidBackground(Color color) {
        .setInvalidBackground(color);
    }

    
    public Color getInvalidBackground() {
        return .getInvalidBackground();
    }

    
    public void setInputValid(boolean valid) {
        .setValid(valid);
    }

    
    public boolean isInputValid() {
        return .isValid();
    }

    
    public void setInvalidText(String text) {
        .setInvalidText(text);
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        .paint(g);
    }
    @Override
    public String toString() {
        return new ToStringBuilder(this).appendSuper(super.toString())
                .toString();
    }
New to GrepCode? Check out our FAQ X