Inline Listeners

J

Jason Cavett

I am added listeners (DocumentListener) to a GUI component
(specifically a class that extends JTextField). I am wondernig why
listeners can be instantiated WITHOUT knowing any of the information
within the listener. For example...

nameTextField.getDocument().addDocumentListener(
new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
if (fireEvent) {
update();
}
}

public void insertUpdate(DocumentEvent e) {
if (fireEvent) {
update();
}
}

public void removeUpdate(DocumentEvent e) {
if (fireEvent) {
update();
}
}

private void update() {

dataModel.setName(nameTextField.getText());
commonUpdate();
nameTextField.verify();
}
});

This listener is added when the GUI is instantiated, however, it is
not until later that I set the dataModel. Why does this work? (I
realize this is probably a fundamental question, but since I never
understood it fully, I figured I would ask.)
 
R

Robert Klemme

I am added listeners (DocumentListener) to a GUI component
(specifically a class that extends JTextField). I am wondernig why
listeners can be instantiated WITHOUT knowing any of the information
within the listener. For example...

nameTextField.getDocument().addDocumentListener(
new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
if (fireEvent) {
update();
}
}

public void insertUpdate(DocumentEvent e) {
if (fireEvent) {
update();
}
}

public void removeUpdate(DocumentEvent e) {
if (fireEvent) {
update();
}
}

private void update() {

dataModel.setName(nameTextField.getText());
commonUpdate();
nameTextField.verify();
}
});

This listener is added when the GUI is instantiated, however, it is
not until later that I set the dataModel. Why does this work? (I
realize this is probably a fundamental question, but since I never
understood it fully, I figured I would ask.)

Your anonymous class inherits a reference to the instance of the
surrounding class. Your method commonUpdate() are invoked on that
instance; same for fields ("nameTextField" in this case).

Kind regards

robert
 
J

Jason Cavett

Your anonymous class inherits a reference to the instance of the
surrounding class. Your method commonUpdate() are invoked on that
instance; same for fields ("nameTextField" in this case).

Kind regards

robert- Hide quoted text -

- Show quoted text -

Cool. Thank you.

Is there any particular reason you would/would not want to do this?
(For example, the reason I did it this way is because every text field
reacts somewhat differently depending on the field.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Is there any particular reason you would/would not want to do this?
(For example, the reason I did it this way is because every text field
reacts somewhat differently depending on the field.

Some people (including me) find the code rather unreadable.

Arne
 
J

Jason Cavett

Some people (including me) find the code rather unreadable.

Arne

Yeah. That's my biggest complaint. But, honestly, I'm not sure of a
better way to go about it. I mean, given the fact that something
different is done based on each text field, button, etc it seems like
I could either:

A. Create a ton of specific classes for each item.
B. Do it inline.
C. ???

If there's another way, I'd be open to hearing it. Listeners have
always given me problems from an OO perspective.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jason said:
Yeah. That's my biggest complaint. But, honestly, I'm not sure of a
better way to go about it. I mean, given the fact that something
different is done based on each text field, button, etc it seems like
I could either:

A. Create a ton of specific classes for each item.
B. Do it inline.
C. ???

If there's another way, I'd be open to hearing it. Listeners have
always given me problems from an OO perspective.

I am not aware of any brilliant solution.

I tend to prefer passing this to all and then have
the actionPerformed test for source.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top