Link to home
Start Free TrialLog in
Avatar of xenia27
xenia27Flag for Taiwan, Province of China

asked on

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???
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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);
   }
}
}
Avatar of prcsn1
prcsn1

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 ...
Avatar of xenia27

ASKER

How can I compare which JTextField it is???
What I try to do is compare the JTextField and set a flag.
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;
    }

----
----
----
}
Avatar of xenia27

ASKER

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)
        {
        }
}
> 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
         
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...

}
Avatar of xenia27

ASKER

a seperate listener <-- what does this mean???
Avatar of xenia27

ASKER

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"...@@
SOLUTION
Avatar of mmuruganandam
mmuruganandam
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xenia27

ASKER

jTF2.getDocument().addDocumentListener(new UpdateTextListener()); <-- this is what I did, right???
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...

}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What flag do you want to set? What object is it member of?
Avatar of xenia27

ASKER

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.
>>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) {
    }
Avatar of xenia27

ASKER

OK...maybe I should ask...how do you know whether the DocumentListener do work or not???
put system.out.println and see whether that is working or not
> how do you know whether the DocumentListener do work or not???

see the example in my first post.
Avatar of xenia27

ASKER

ok...I solve my problem....@@...my bad...the one I tested on is not with a separate listener...
Sorry~~~
Avatar of xenia27

ASKER

one more question.....Is there something like "EnterCriticalSection" in Java??  How can you make sure there is only thread change a value each time???
synchronized(...)
{
 // do the work...
}
> 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)
{
}
Avatar of xenia27

ASKER

Thanks~~~  ^^
are you able to make it work..
Avatar of xenia27

ASKER

Yes...finally...it's working...^^
that's great....
Thanks