xenia27
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().add DocumentLi stener(new UpdateTextListener());
class UpdateTextListener implements DocumentListener
{
public void changedUpdate(DocumentEven t 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???
Here are my codes..
JTextField tempText = new JTextField();
tempText.getDocument().add
class UpdateTextListener implements DocumentListener
{
public void changedUpdate(DocumentEven
{
// here I set some flags <--- set breakpoints
}
public void insertUpdate(DocumentEvent
{
// here I set some flags <-- set breakpoints
}
public void removeUpdate(DocumentEvent
{
// 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???
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 ...
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 ...
ASKER
How can I compare which JTextField it is???
What I try to do is compare the JTextField and set a flag.
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().addDocume ntListener (new UpdateTextListener("textfi eld1"));
JTextField tf1 = new JTextField();
tf1.getDocument().addDocum entListene r(new UpdateTextListener("textfi eld2"));
class UpdateTextListener implements DocumentListener
{
private String textFieldName ;
UpdateTextListener (String textfield)
{
textFieldName = textfield;
}
----
----
----
}
you have an option
JTextField tf = new JTextField();
tf.getDocument().addDocume
JTextField tf1 = new JTextField();
tf1.getDocument().addDocum
class UpdateTextListener implements DocumentListener
{
private String textFieldName ;
UpdateTextListener (String textfield)
{
textFieldName = textfield;
}
----
----
----
}
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(BorderFacto ry.createE tchedBorde r());
jTF1.setBounds(10, 80, 40, 20);
jTF1.setHorizontalAlignmen t(SwingCon stants.LEF T);
jTF1.getDocument().addDocu mentListen er(new UpdateTextListener());
JTextField jTF2 = new JTextField();
jTF2.setBorder(BorderFacto ry.createE tchedBorde r());
jTF2.setBounds(10, 80, 40, 20);
jTF2.setHorizontalAlignmen t(SwingCon stants.LEF T);
jTF2.getDocument().addDocu mentListen er(new UpdateTextListener());
BasicPanel.add(jTF1);
BasicPanel.add(jTF2);
getContentPane().add(Basic Panel);
public class UpdateTextListener implements DocumentListener
{
public void changedUpdate(DocumentEven t 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)
{
}
}
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(BorderFacto
jTF1.setBounds(10, 80, 40, 20);
jTF1.setHorizontalAlignmen
jTF1.getDocument().addDocu
JTextField jTF2 = new JTextField();
jTF2.setBorder(BorderFacto
jTF2.setBounds(10, 80, 40, 20);
jTF2.setHorizontalAlignmen
jTF2.getDocument().addDocu
BasicPanel.add(jTF1);
BasicPanel.add(jTF2);
getContentPane().add(Basic
public class UpdateTextListener implements DocumentListener
{
public void changedUpdate(DocumentEven
{
// how can I compare which JTextField???
}
public void insertUpdate(DocumentEvent
{
// how can I compare which JTextField I insert?
}
public void removeUpdate(DocumentEvent
{
}
}
> 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
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...
}
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...
}
ASKER
a seperate listener <-- what does this mean???
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.Document Event"...@ @
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
jTF2.getDocument().addDocu mentListen er(new UpdateTextListener()); <-- this is what I did, right???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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().addDocume ntListener (new UpdateTextListener("textfi eld1"));
JTextField tf1 = new JTextField();
tf1.getDocument().addDocum entListene r(new UpdateTextListener("textfi eld2"));
class UpdateTextListener implements DocumentListener
{
private String textFieldName ;
UpdateTextListener (String textfield)
{
textFieldName = textfield;
}
xxx(DocumentEvent e)
{
if (textFieldName.equals("tex tfield1"))
{
// do something...
}
else if (textFieldName.equals("tex tfield2"))
{
// do something
}
and so on...
}
>>I don't know why but when I try to use "e.getSource()"
Then follow the below approach...
JTextField tf = new JTextField();
tf.getDocument().addDocume
JTextField tf1 = new JTextField();
tf1.getDocument().addDocum
class UpdateTextListener implements DocumentListener
{
private String textFieldName ;
UpdateTextListener (String textfield)
{
textFieldName = textfield;
}
xxx(DocumentEvent e)
{
if (textFieldName.equals("tex
{
// do something...
}
else if (textFieldName.equals("tex
{
// do something
}
and so on...
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What flag do you want to set? What object is it member of?
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...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(DocumentEven t e) {
}
public void insertUpdate(DocumentEvent e) {
}
public void removeUpdate(DocumentEvent e) {
}
xxx->
public void changedUpdate(DocumentEven
}
public void insertUpdate(DocumentEvent
}
public void removeUpdate(DocumentEvent
}
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.
see the example in my first post.
ASKER
ok...I solve my problem....@@...my bad...the one I tested on is not with a separate listener...
Sorry~~~
Sorry~~~
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...
}
{
// 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)
{
}
Use a synchronized method or block.
method:
public void synchronized someMethod()
{
...
block:
synchronized (somevar)
{
}
ASKER
Thanks~~~ ^^
are you able to make it work..
ASKER
Yes...finally...it's working...^^
that's great....
:)
Thanks
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().addDocume
f.getContentPane().add(tf)
f.pack();
f.show();
}
static class UpdateTextListener implements DocumentListener
{
public void changedUpdate(DocumentEven
{
System.out.println(e);
}
public void insertUpdate(DocumentEvent
{
System.out.println(e);
}
public void removeUpdate(DocumentEvent
{
System.out.println(e);
}
}
}