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

  • SEARCH
  • Home
  • 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 4024950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:47:09+00:00 2026-05-20T10:47:09+00:00

Is it possible to discover which oject generated a DocumentEvent? Something like i can

  • 0

Is it possible to discover which oject generated a DocumentEvent? Something like i can do with ActionListener:

JTextField field = new JTextField("");
field.addActionListener(actionListener);

//inside ActionListener
public void actionPerformed(ActionEvent arg0) {
  if (arg0.getSource() instanceof JTextField) //true

}

I would like to do the same with DocumentEvent but seems not to work the same way:

JTextField field = new JTextField("");
field.getDocument.addDocumentListener(documentListener);
//inside documentListener
public void insertUpdate(DocumentEvent){
  if (arg0.getSource() instanceof JTextField) //false: class is javax.swing.text.PlainDocument
  if (arg0.getSource() instanceof MyComponent){
      MyComponent comp = (MyComponent)arg0.getSource();
      comp.callSpecificMethodUponMyComp(); 
  }
}

The answser should take in consideration the following points:

  1. For my purposes knowing the object type that generated the event isn’t enough, but i need a reference to it at runtime.
  2. Typically the events are generated from extension of the swing object (myComp public JTextField). These objects store additional information that should be retrieved at runtime inside the listener method (es. insertUpdate)
  3. DocumentListener is implemented from a class that is unaware of the fields that generated the events. Differents fields of differents types can be attacched to the listener at runtime.
  • 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-05-20T10:47:09+00:00Added an answer on May 20, 2026 at 10:47 am

    You can set a property in the document to tell you which textcomponent the document belongs to:

    For example:

    final JTextField field = new JTextField("");
    field.getDocument().putProperty("owner", field); //set the owner
    
    final JTextField field2 = new JTextField("");
    field2.getDocument().putProperty("owner", field2); //set the owner
    
    DocumentListener documentListener = new DocumentListener() {
    
         public void changedUpdate(DocumentEvent documentEvent) {}
    
         public void insertUpdate(DocumentEvent documentEvent) {
    
             //get the owner of this document
             Object owner = documentEvent.getDocument().getProperty("owner");
             if(owner != null){
                 //owner is the jtextfield
                 System.out.println(owner);
             }
         }
    
         public void removeUpdate(DocumentEvent documentEvent) {}
    
         private void updateValue(DocumentEvent documentEvent) {}
    };
    
    field.getDocument().addDocumentListener(documentListener);
    field2.getDocument().addDocumentListener(documentListener);
    

    Alternatively:

    Get the document that sourced the event and compare it to the document of the textfield.

    Example:

    public void insertUpdate(DocumentEvent documentEvent) {
        if (documentEvent.getDocument()== field.getDocument()){
            System.out.println("event caused by field");
        }
        else if (documentEvent.getDocument()== field2.getDocument()){
            System.out.println("event caused by field2");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wanting to discover what possible standard .net exceptions can cause stack traces
I'm just starting to discover Three20, and would like to know in which direction
Possible Duplicate: New Cool Features of C# 4.0 Hello, There are several(many) questions at
I have a side panel like widget in my app that can be swiped
Possible Duplicate: Special (magic) methods in Python who can tell me what can call
If we discover bug in some branche we fix it (check New release on
Is possible to discover the iOS device identifier, using Xcode. I need to each
i need to discover all possible combinations for some arrays that have an specific
I'm working on a webpage on which I want to discover some UPnP devices,
Is it possible from code to discover the interval a previous alarm was set

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.