请查看以下3组课程。请注意,仅显示相关代码,而其余大部分则被删除。

CommonGUI.java

package normal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
import javax.swing.text.AbstractDocument;

public class CommonGUI extends JPanel
{
    protected JLabel name, mobileNumber1, mobileNumber2, address, landNumber1, landNumber2,  nickName, email, category;
    protected JTextField nameTxt, mobileNumber1Txt, mobileNumber2Txt, landNumber1Txt, landNumber2Txt, nickNameTxt, emailTxt, categoryTxt;

    protected JTextArea addressTxt;
    protected JScrollPane scroll;


    protected JComboBox categoryCombo;


    public CommonGUI()
    {
        //Intializing the instance variables
        name = new JLabel("Name: ");
        mobileNumber1 = new JLabel("Mobile Number 1:");
        mobileNumber2 = new JLabel("Mobile Number 2: ");
        address = new JLabel("Address: ");
        landNumber1 = new JLabel("Land Number 1: ");
        landNumber2 = new JLabel("Land Number 2: ");
        nickName = new JLabel("Nick Name: ");
        email = new JLabel("Email: ");
        category = new JLabel("Category: ");

        nameTxt = new JTextField(10);
        mobileNumber1Txt = new JTextField(10);
        mobileNumber2Txt = new JTextField(10);
        addressTxt = new JTextArea(5,20);
        landNumber1Txt = new JTextField(10);
        landNumber2Txt = new JTextField(10);
        categoryTxt = new JTextField();
        nickNameTxt = new JTextField(10);
        emailTxt = new JTextField(10);

        categoryCombo = new JComboBox();



        //Adding document listeners to text fields

        AbstractDocument[]d = new AbstractDocument[8];
         d[0] = (AbstractDocument) nameTxt.getDocument();
         d[1] = (AbstractDocument) mobileNumber1Txt.getDocument();
         d[2] = (AbstractDocument) mobileNumber2Txt.getDocument();
         d[3] = (AbstractDocument) addressTxt.getDocument();
         d[4] = (AbstractDocument) landNumber1Txt.getDocument();
         d[5] = (AbstractDocument) landNumber2Txt.getDocument();
         d[6] = (AbstractDocument) categoryTxt.getDocument();
         d[7] = (AbstractDocument) nickNameTxt.getDocument();

         d[0].setDocumentFilter(new TextFieldValidator(nameTxt,20));
         d[1].setDocumentFilter(new TextFieldValidator(mobileNumber1Txt,10));
         d[2].setDocumentFilter(new TextFieldValidator(mobileNumber2Txt,10));
         d[3].setDocumentFilter(new TextFieldValidator(addressTxt,100));
         d[4].setDocumentFilter(new TextFieldValidator(landNumber1Txt,10));
         d[5].setDocumentFilter(new TextFieldValidator(landNumber2Txt,10));
         d[6].setDocumentFilter(new TextFieldValidator(categoryTxt,20));
         d[7].setDocumentFilter(new TextFieldValidator(nickNameTxt,20));
    }

}


UpdateDeleteForm.java

//Following class get fired when an "category" is selected from the JComboBox. Rest of the code is ommited

    private class DetailsLoader implements ItemListener
        {
            public void itemStateChanged(ItemEvent ie)
            {
                if(ie.getStateChange()==ItemEvent.SELECTED)
                {
                    List details = new ArrayList();


                    if(nameTxtCombo.getSelectedItem().toString() != "Select a Name")
                    {
                    details = dateBaseConnector.getDetails(nameTxtCombo.getSelectedItem().toString());

                    String address = (String)details.get(6);


                    idTxt.setText(String.valueOf(details.get(0)));
                    nameTxt.setText((String)details.get(1));
                    mobileNumber1Txt.setText(String.valueOf(details.get(2)));
                    mobileNumber2Txt.setText(String.valueOf(details.get(3)));
                    landNumber1Txt.setText(String.valueOf(details.get(4)));
                    landNumber2Txt.setText(String.valueOf(details.get(5)));
                    addressTxt.setText(address.trim());
                    //categoryCombo.setSelectedItem((String)details.get(7));
                    nickNameTxt.setText((String)details.get(8));
                    emailTxt.setText((String)details.get(9));


                    }

                }
            }
        }


TextFieldValidator.java

package normal;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.JTextComponent;

public class TextFieldValidator extends DocumentFilter
{
    private JTextComponent textField;
    private int numberOfLetters;

    public TextFieldValidator(JTextComponent text, int i)
    {
        textField = text;
        numberOfLetters = i;
    }

    @Override
    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)  throws BadLocationException
    {
        if(fb.getDocument().getLength()+string.length()>numberOfLetters)
        {
            return;
        }

        super.insertString(fb, offset, string, attr);
    }

    @Override
    public void remove(FilterBypass fb, int offset, int length) throws BadLocationException
    {
        fb.remove(offset, length);
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException
    {
        if(fb.getDocument().getLength()+text.length()>numberOfLetters)
        {
            return;
        }

        super.insertString(fb, offset, text, attrs);
    }
}


现在,当我选择一个类别时,它将根据该类别中选择的项目填充其他文本字段。但是,当我添加documentListener来添加验证时,setText()方法的行为异常。这意味着可以想象一下


第一次-从类别中选择“大学”。它将nameTxt框填充为“ Yohan”
第二次-现在您需要查看另一个,从类别中选择“学院”。现在,它应该在Txt名称中显示“ Brian”,但是,它仍然显示了“ Brian Yohan”。嗯..“ Yohan”尚未从文本字段中删除!


当我从验证中删除documentListener时,一切正常,没有错误。因此,我相信这与documentListener有关。请帮忙!!

最佳答案

问题是您的replace方法:

 @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException
    {
        if(fb.getDocument().getLength()+text.length()>numberOfLetters)
        {
            return;
        }

        super.insertString(fb, offset, text, attrs);
    }


您在这里呼叫super.insertString而不是呼叫super.replace。您的setText调用将调用此replace方法,并最终插入文本而不是替换文本。

关于java - DocumentListener在JTextField.setText()中出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12112694/

10-09 07:39