验证文本字段并联系无文本字段 [英] Validation of text fields and contact no text field

查看:160
本文介绍了验证文本字段并联系无文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JFrame ,包含一些文本字段(10)和一个TextArea。我想验证所有文本字段并查看它们是否为空,并检查是否在其中一个文本字段中输入了10位数的联系人号码。检查文本字段后,我想启用一个提交按钮,用于将所有这些数据提交到我的数据库。

I have a JFrame consisting of some text fields (10) and a TextArea. I want to validate all the text fields and see if they are not empty and also check if a 10 digit contact no is entered in one of the text field. After checking the text fields, I want to enable a submit button which is used to submit all this dat to my database.

我使用以下代码添加文本区域条件但它不起作用,给出错误: - 线程AWT-EventQueue-0java.lang中的异常。 NullPointerException

I used following code with adding the text area condition but it is not working,gives the error:- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

这是我使用的代码,但它不起作用: -

Here is the code i used but it is not working:-

public class DataEntered1 implements DocumentListener
{
    private JButton button;
    List<JTextField> txtfields=new ArrayList<JTextField>();
    JTextArea ta;
    public DataEntered1(JButton dbadd)
    {
        this.button=dbadd;
    }
    public void addTextField(JTextField txtfield)
    {
        txtfields.add(txtfield);
        txtfield.getDocument().addDocumentListener(this);
    }
    public void addTextArea(JTextArea ta)
    {
        this.ta=ta;
        ta.getDocument().addDocumentListener(this);
    }
    public boolean isDataEntered()
    {
        for(JTextField txtfield:txtfields)
        {
            if(txtfield.getText().length()==0)
            return false;
        }
        return true;
    }
    public boolean isData()
    {
        if(ta.getText().trim().length()==0)
        {
             return false;
        }
        return true;
    }
    public void insertUpdate(DocumentEvent e) {
    checkdata();
    }
    public void removeUpdate(DocumentEvent e) {
    checkdata();
    }
    public void changedUpdate(DocumentEvent e) {
    checkdata();
    }
    public void checkdata(){
    Boolean d1=isDataEntered();
    Boolean d2=isData();
    if(d1&&d2)
    button.setEnabled(true);
    }

}

推荐答案


在验证所有文本字段后,有人可以帮助我启用按钮吗?

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.

它添加了一个DocumentListener到每个文本字段的Documenent。只有当文本输入到每个文档时,该按钮才会启用:

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();
            }
        });
    }
}




还检查是否在其中一个文本字段中输入10位联系号。

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

您需要自定义isDataEntered()方法以添加检查这个额外的要求。

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

这篇关于验证文本字段并联系无文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆