documentlistener在jtextfield.settext()中出现问题

kyxcudwk  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(406)

请看下面三组课程。请注意,只有相关的代码显示,而其余的大多数被删除。
通用图形用户界面.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”
第二次-现在您需要查看另一个,从类别中选择“学院”。现在,它应该在nametxt中显示“brian”,但除此之外,它还显示“brian yohan”。隐马尔可夫模型。。“yohan”还没有从文本字段中删除!!
当我从验证中删除documentlistener时,一切正常,没有错误。所以,我相信这和我的听众有关。请帮忙!!

x9ybnkn6

x9ybnkn61#

问题是你的 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 call会叫这个 replace 方法并最终插入文本而不是替换它。

7gcisfzg

7gcisfzg2#

当我从验证中删除documentlistener时,一切正常,没有错误。所以,我相信这和我的听众有关
我看不出有什么理由 DocumentistenerDocumentFilter 一起,因为两个(一起)可能会触发无数相同的(相似的)事件,然后其中一个调用第二个
基本材料: DocumentModel 为了 JTextComponent s DocumentListener 指定用于输出 JTextComponent 外部 DocumentFilter 用于过滤不需要的 Char(s) 从中键入或插入的 System ClipboardJTextComponents ,您可以从 Document 方法或gui的其余部分,在插入或键入 Char(s )通过
为了获得更好的帮助,请尽快发布一个sscce,并详细描述您的预期事件 JComboBoxJTextField 反之亦然

相关问题