Solved

Document Listener

Posted on 2004-04-26
30
358 Views
Last Modified: 2010-03-31
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
Comment
Question by:xenia27
  • 11
  • 9
  • 6
  • +2
30 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10924928
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
 
LVL 2

Expert Comment

by:prcsn1
ID: 10925052
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
 

Author Comment

by:xenia27
ID: 10925275
How can I compare which JTextField it is???
What I try to do is compare the JTextField and set a flag.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10925404
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
 

Author Comment

by:xenia27
ID: 10925876
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
 
LVL 92

Expert Comment

by:objects
ID: 10925917
> 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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10925941
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
 

Author Comment

by:xenia27
ID: 10925949
a seperate listener <-- what does this mean???
0
 

Author Comment

by:xenia27
ID: 10925968
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
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 200 total points
ID: 10925969
jTF2.getDocument().addDocumentListener(new UpdateTextListener());// is a separate listener

jTF1.getDocument().addDocumentListener(new UpdateTextListener());// is a separate listener
0
 

Author Comment

by:xenia27
ID: 10925975
jTF2.getDocument().addDocumentListener(new UpdateTextListener()); <-- this is what I did, right???
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 10926003
> 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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10926005
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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
ID: 10926023
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10926029
What flag do you want to set? What object is it member of?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:xenia27
ID: 10926049
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10926067
>>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
 

Author Comment

by:xenia27
ID: 10926151
OK...maybe I should ask...how do you know whether the DocumentListener do work or not???
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10926176
put system.out.println and see whether that is working or not
0
 
LVL 92

Expert Comment

by:objects
ID: 10926188
> how do you know whether the DocumentListener do work or not???

see the example in my first post.
0
 

Author Comment

by:xenia27
ID: 10926199
ok...I solve my problem....@@...my bad...the one I tested on is not with a separate listener...
Sorry~~~
0
 

Author Comment

by:xenia27
ID: 10926212
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10926229
synchronized(...)
{
 // do the work...
}
0
 
LVL 92

Expert Comment

by:objects
ID: 10926243
> 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
 

Author Comment

by:xenia27
ID: 10926747
Thanks~~~  ^^
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10926760
are you able to make it work..
0
 

Author Comment

by:xenia27
ID: 10926766
Yes...finally...it's working...^^
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10926769
that's great....
0
 
LVL 92

Expert Comment

by:objects
ID: 10926875
:)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10927060
Thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now