Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

Document Listener

I try to use DocumentListenter so when the content of my JTextField changes I can update the information correctly.

Here are my codes..

JTextField  tempText = new JTextField();
tempText.getDocument().addDocumentListener(new UpdateTextListener());

class UpdateTextListener implements DocumentListener
{
  public void changedUpdate(DocumentEvent e)
  {
    // here I set some flags  <---  set breakpoints
  }
  public void insertUpdate(DocumentEvent e)
  {
     // here I set some flags  <-- set breakpoints
  }
  public void removeUpdate(DocumentEvent e)
  {
    // here I set some flags
   }
}

I'm using Visual Cafe and set some breakpoints so I can see the flags are changed when the content of JTextFields are changed.  But I didn't run into the breakpoints.  What's wrong???
0
xenia27
Asked:
xenia27
  • 11
  • 9
  • 6
  • +2
3 Solutions
 
objectsCommented:
not sure, the following works fine:

import javax.swing.*;
import javax.swing.event.*;

public class DocTest
{
      public static void main(String[] args)
      {
            JFrame f = new JFrame();
            JTextField  tf = new JTextField();
            tf.getDocument().addDocumentListener(new UpdateTextListener());
            f.getContentPane().add(tf);
            f.pack();
            f.show();
      }
      
static class UpdateTextListener implements DocumentListener
{
  public void changedUpdate(DocumentEvent e)
  {
    System.out.println(e);
  }
  public void insertUpdate(DocumentEvent e)
  {
    System.out.println(e);
  }
  public void removeUpdate(DocumentEvent e)
  {
    System.out.println(e);
   }
}
}
0
 
prcsn1Commented:
I'm rather suspicious you are not quite setting up correctly.
Can we have the complete source code please - or, preferably a cut down version that demonstrates the problem. Does "objects" code work? It looks good to me ...
0
 
xenia27Author Commented:
How can I compare which JTextField it is???
What I try to do is compare the JTextField and set a flag.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mmuruganandamCommented:
for comparing the text field...

you have an option

JTextField  tf = new JTextField();
tf.getDocument().addDocumentListener(new UpdateTextListener("textfield1"));

JTextField  tf1 = new JTextField();
tf1.getDocument().addDocumentListener(new UpdateTextListener("textfield2"));

class UpdateTextListener implements DocumentListener
{
    private String textFieldName ;

    UpdateTextListener (String textfield)
    {
         textFieldName = textfield;
    }

----
----
----
}
0
 
xenia27Author Commented:
mMmm...not sure how will that work...
For example...I have several JTextField..
JTextField  jTF1, jTF2, jTF3, jTF4;

so how can I compare which jTF???

Here are my codes:
JPanel BasicPanel = new JPanel();
JTextField jTF1 = new JTextField();
jTF1.setBorder(BorderFactory.createEtchedBorder());         
jTF1.setBounds(10, 80, 40, 20);
jTF1.setHorizontalAlignment(SwingConstants.LEFT);
jTF1.getDocument().addDocumentListener(new UpdateTextListener());

JTextField jTF2 = new JTextField();
jTF2.setBorder(BorderFactory.createEtchedBorder());         
jTF2.setBounds(10, 80, 40, 20);
jTF2.setHorizontalAlignment(SwingConstants.LEFT);
jTF2.getDocument().addDocumentListener(new UpdateTextListener());

BasicPanel.add(jTF1);
BasicPanel.add(jTF2);
getContentPane().add(BasicPanel);


public class UpdateTextListener implements DocumentListener
{
      public void changedUpdate(DocumentEvent e)
      {
              // how can I compare which JTextField???
       }
          
         public void insertUpdate(DocumentEvent e)
        {
            // how can I compare which JTextField I insert?
        }

        public void removeUpdate(DocumentEvent e)
        {
        }
}
0
 
objectsCommented:
> What I try to do is compare the JTextField and set a flag.

you could use a seperate listener for each field
alternatively use e.getSource() to see where the event came from
         
0
 
mmuruganandamCommented:
Separate listener what i post previously...

in the update document listener


xxx(DocumentEvent e)
{
   Object source = e.getSource();
   
   if (source instanceof jTF1)
   {
      // do something...
   }
   else if (source instanceof jTF2)
   {
      // do something
   }
  and so on...

}
0
 
xenia27Author Commented:
a seperate listener <-- what does this mean???
0
 
xenia27Author Commented:
I don't know why but when I try to use "e.getSource()"  I got complie error..."Method getSource() not found in javax.swing.event.DocumentEvent"...@@
0
 
mmuruganandamCommented:
jTF2.getDocument().addDocumentListener(new UpdateTextListener());// is a separate listener

jTF1.getDocument().addDocumentListener(new UpdateTextListener());// is a separate listener
0
 
xenia27Author Commented:
jTF2.getDocument().addDocumentListener(new UpdateTextListener()); <-- this is what I did, right???
0
 
objectsCommented:
> I don't know why but when I try to use "e.getSource()"  I got complie error..."Method getSource() not found in javax.swing.event.DocumentEvent"...@@

My bad, forgot that DocumentEvent's were slightly different.
Use:

Document doc = e.getDocument();

you can then compare that Document with the document of your tf's.
eg.

if (doc==tf1.getDocument())
{
   // tf1 changed
}
else if (doc==tf2.getDocument())
{
   // tf2 changed
}
etc.

or use seperate listeners, whichever is most appropriate to your situation :)
0
 
mmuruganandamCommented:
yep... u r right...


>>I don't know why but when I try to use "e.getSource()"  

Then follow the below approach...


JTextField  tf = new JTextField();
tf.getDocument().addDocumentListener(new UpdateTextListener("textfield1"));

JTextField  tf1 = new JTextField();
tf1.getDocument().addDocumentListener(new UpdateTextListener("textfield2"));

class UpdateTextListener implements DocumentListener
{
    private String textFieldName ;

    UpdateTextListener (String textfield)
    {
         textFieldName = textfield;
    }


xxx(DocumentEvent e)
{
   if (textFieldName.equals("textfield1"))
   {
      // do something...
   }
   else if (textFieldName.equals("textfield2"))
   {
      // do something
   }
  and so on...

}
0
 
zzynxSoftware engineerCommented:
A getSource() doesn't exist for DocumentEvent. (like it does for the EventObject)

Of course you can counter that by creating a custom textField listening to itself:

class MyTextField extends JTextField implements DocumentListener {

    public MyTextField() {
       super();
       getDocument().addDocumentListener(this);
    }

    // DocumentListener interface
    public void changedUpdate(DocumentEvent e) {
       // "'this" is the JTextField that has changed
    }
    public void insertUpdate(DocumentEvent e) {
    }
    public void removeUpdate(DocumentEvent e) {
    }
}
0
 
zzynxSoftware engineerCommented:
What flag do you want to set? What object is it member of?
0
 
xenia27Author Commented:
mmuruganandam,
I do have xxx(DocumentEvent e) in my functions...my bad...@@

zzynx,
I just try to set some boolean flags when the text is inserted, removed, or changed so later when I click on a "Update" button, it will check which one is changed and update the information.
0
 
mmuruganandamCommented:
>>I do have xxx(DocumentEvent e) in my functions
xxx->
   public void changedUpdate(DocumentEvent e) {
    }
    public void insertUpdate(DocumentEvent e) {
    }
    public void removeUpdate(DocumentEvent e) {
    }
0
 
xenia27Author Commented:
OK...maybe I should ask...how do you know whether the DocumentListener do work or not???
0
 
mmuruganandamCommented:
put system.out.println and see whether that is working or not
0
 
objectsCommented:
> how do you know whether the DocumentListener do work or not???

see the example in my first post.
0
 
xenia27Author Commented:
ok...I solve my problem....@@...my bad...the one I tested on is not with a separate listener...
Sorry~~~
0
 
xenia27Author Commented:
one more question.....Is there something like "EnterCriticalSection" in Java??  How can you make sure there is only thread change a value each time???
0
 
mmuruganandamCommented:
synchronized(...)
{
 // do the work...
}
0
 
objectsCommented:
> How can you make sure there is only thread change a value each time???

Use a synchronized method or block.

method:

public void synchronized someMethod()
{
   ...

block:

synchronized (somevar)
{
}
0
 
xenia27Author Commented:
Thanks~~~  ^^
0
 
mmuruganandamCommented:
are you able to make it work..
0
 
xenia27Author Commented:
Yes...finally...it's working...^^
0
 
mmuruganandamCommented:
that's great....
0
 
objectsCommented:
:)
0
 
zzynxSoftware engineerCommented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 11
  • 9
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now