Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8425813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:22:35+00:00 2026-06-10T04:22:35+00:00

Please have a look at the following 3 set of classes. Please note that

  • 0

Please have a look at the following 3 set of classes. Please note that only relevant code is shown, while rest of the majority is removed.

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

Now, when I select an category, it fills up the other text fields according to the item selected in the category. However, when I add the documentListener to add the validations, the setText() methods behave unexpectedly. Which means imagine this,

  1. First Time – Select “University” from the category. It fills the nameTxt box as “Yohan”

  2. Second Time – Now you need to view another one, select “College” from the category. Now, it should show “Brian” in the nameTxt, but instaed of that, it shows “Brian Yohan”. hmm.. The “Yohan” hasn’t removed from the text field!!

When I remove the documentListener from validation, everything works fine, no errors. So, I believe this is something I got to do with the documentListener. Please help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:22:37+00:00Added an answer on June 10, 2026 at 4:22 am

    The problem is your replace method:

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

    You’re calling super.insertString here instead of calling super.replace. Your setText call is going to call this replace method and end up inserting the text instead of replacing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
am new here. i have a slight problem; PLease look at the following code
Please have a look at the following code package normal; import java.io.*; import java.util.*;
I have HttpClient 4.1. Please have a look at following program: import org.apache.http.client.methods.*; import
In my current project, Workflows have comments. Please have a look at the following
Please have a look at this form that I am trying to design with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.