?
Solved

Document Listener

Posted on 2004-04-26
30
Medium Priority
?
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 800 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 800 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 400 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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

743 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