Learning The "J"

Home » JAVA Learning » SWINGS » Input Text Validation » Input Text Validation in JTextBox

Input Text Validation in JTextBox

To validate the JTextFieldinput ,its easy to use “setDocument” method of JTextField. For this you have to create your validation class which extends javax.swing.text.Plaindocument and write your validation code inside it. then add it as, JTextField.setDocument(userdefinedclass). Its much more flexible then using lostfocus and other events.


import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextInput extends JFrame {

    JPanel contentPane;
    JPanel jPanel1 = new JPanel();
    FlowLayout flowLayout1 = new FlowLayout();
    GridLayout gridLayout1 = new GridLayout();
    JLabel modelLabel = new JLabel();
    JTextField modelText = new JTextField(20);

    public TextInput() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(flowLayout1);
        this.setSize(new Dimension(700, 500));
        this.setTitle("Input Validation");
        jPanel1.setLayout(gridLayout1);
        gridLayout1.setRows(4);
        gridLayout1.setColumns(2);
        gridLayout1.setHgap(20);

        modelLabel.setText("Model");

        IntegerDocument integerDocument1 = new IntegerDocument();
        modelText.setDocument(integerDocument1);


        contentPane.add(jPanel1);
        jPanel1.add(modelLabel);
        jPanel1.add(modelText);
    }

    public static void main(String args[]) {
        TextInput frame = new TextInput();
        frame.pack();
        frame.show();
    }

    class IntegerDocument extends PlainDocument {

        public void insertString(int offset, String string, AttributeSet attributes) throws BadLocationException {

            if (string == null) {
                return;
            } else {
                String newValue;
                int length = getLength();
                if (length == 0) {
                    newValue = string;
                } else {
                    String currentContent = getText(0, length);
                    StringBuffer currentBuffer = new StringBuffer(currentContent);
                    currentBuffer.insert(offset, string);
                    newValue = currentBuffer.toString();
                }
                try {
                    checkInput(newValue);
                    super.insertString(offset, string, attributes);
                } catch (Exception exception) {
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        }

        public void remove(int offset, int length)
                throws BadLocationException {

            int currentLength = getLength();
            String currentContent = getText(0,
                    currentLength);
            String before = currentContent.substring(
                    0, offset);
            String after = currentContent.substring(
                    length + offset, currentLength);
            String newValue = before + after;
            try {
                checkInput(newValue);
                super.remove(offset, length);
            } catch (Exception exception) {
                Toolkit.getDefaultToolkit().beep();
            }
        }

        private int checkInput(String proposedValue)
                throws NumberFormatException {

            int newValue = 0;
            if (proposedValue.length() > 0) {
                newValue = Integer.parseInt(
                        proposedValue);
            }
            return newValue;
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Start here

%d bloggers like this: