Validation of text fields and contact no text field

最后都变了- 提交于 2019-11-29 16:56:08

Can anyone help me about enabling the button after validating all the text fields?

Here is a general purpose class that will enable/disable a button as text is added/removed from a group of text fields.

It adds a DocumentListener to the Documenent of each text field. The button will then ben enable only when text has been entered into each Document:

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;

public class DataEntered implements DocumentListener
{
    private JButton button;
    private List<JTextField> textFields = new ArrayList<JTextField>();

    public DataEntered(JButton button)
    {
        this.button = button;
    }

    public void addTextField(JTextField textField)
    {
        textFields.add( textField );
        textField.getDocument().addDocumentListener( this );
    }

    public boolean isDataEntered()
    {
        for (JTextField textField : textFields)
        {
            if (textField.getText().trim().length() == 0)
                return false;
        }

        return true;
    }

    @Override
    public void insertUpdate(DocumentEvent e)
    {
        checkData();
    }

    @Override
    public void removeUpdate(DocumentEvent e)
    {
        checkData();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {}

    private void checkData()
    {
        button.setEnabled( isDataEntered() );
    }

    private static void createAndShowUI()
    {
        JButton submit = new JButton( "Submit" );
        submit.setEnabled( false );

        JTextField textField1 = new JTextField(10);
        JTextField textField2 = new JTextField(10);

        DataEntered de = new DataEntered( submit );
        de.addTextField( textField1 );
        de.addTextField( textField2 );

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textField1, BorderLayout.WEST);
        frame.add(textField2, BorderLayout.EAST);
        frame.add(submit, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

also check if a 10 digit contact no is entered in one of the text field.

You would need to customize the isDataEntered() method to add a check for this additional requirement.

Validating a textfield is empty of not can be done by getting text from the textview and comparing it to ""

Suppose your TextField is textField.

if (textField.getText().trim().length>0) {
    //TextField is empty
} else {
    //TextField is not empty
}

Similarly if you want to see a 10 digit contact number.

if (textfield.getText().length == 10) {
    /*
     Here I'm not checking whether each character is a digit, 
     but you can do so by iterating through each character and checking
     whether it's a digit using isDigit() method
    */
} else {
    //Not 10 characters
} 

The appropriate listener in Java's swing to track changes in the text content of a JTextField is a DocumentListener, that you have to add to the document of the JTextField:

textField.getDocument().addDocumentListener(new DocumentListener() {
    // Enable the buttons here. 
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!