Reputation: 31
I'm trying to write a simple retirement calculator for a project.
I'm trying to get a document listener so I can take input from different text fields and use them in my calculations.
For some reason I can't get the DocumentListener to instantiate, and I get an error. I think that I lack understanding of how to do this.
The specific line of code that is giving me an error is this:
DocumentListener DL = new DocumentListener();
Here is the class where I try to use this:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class TextFields extends JPanel implements DocumentListener
{
public TextFields()
{
setLayout(new GridLayout(5,1));
setFont(new Font("Tahoma", Font.PLAIN, 14));
DocumentListener DL = new DocumentListener();
JTextField age = new JTextField("Age");
age.getDocument().addDocumentListener(DL);
JLabel ageLabel = new JLabel("Age: ");
JTextField initialSavings = new JTextField("Intial Savings");
JLabel ISLabel = new JLabel("Inital Savings: ");
JTextField ageRetire = new JTextField("Retirement Age");
JLabel RALabel = new JLabel("Retirement Age: ");
JTextField inflation = new JTextField("Inflation Rate");
JLabel inflationLabel = new JLabel("Inflation Rate: ");
JTextField dailySavings = new JTextField("Daily Savings");
JLabel DSLabel = new JLabel("Daily Savings: ");
JTextField DeathAge = new JTextField("Age Of Death");
JLabel DALabel = new JLabel("When will you die? ");
JTextField retirementIncome = new JTextField("Retirement Income:");
JLabel RILabel = new JLabel("Retirement Income: ");
JTextField interest = new JTextField("Interest Rate");
JLabel interestLabel = new JLabel("Interest: ");
add(ageLabel);
add(age);
add(ISLabel);
add(initialSavings);
add(RALabel);
add(ageRetire);
add(inflationLabel);
add(inflation);
add(DSLabel);
add(dailySavings);
add(DALabel);
add(DeathAge);
add(interestLabel);
add(interest);
add(RILabel);
add(retirementIncome);
}
@Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
}
Since I am beginning programer, I would appreciate simple answers that won't require a huge amount knowledge to understand.
Thank you very much for your time and help! Roy.
Upvotes: 1
Views: 2317
Reputation: 1415
As you can see in your TextFields class declaration you are implementing DocumentLstener which means that's an interface and you can't instantiate an interface. replace the problematic line with this
DocumentListener DL = new TextFields();
Since TextFields has implemented the DocumentListener interface everything will be fine. Or you can use a separate or inner class for listener.
Upvotes: 0
Reputation: 8960
Short version:
...
// DocumentListener DL = new DocumentListener();
JTextField age = new JTextField("Age");
age.getDocument().addDocumentListener(this);
...
Long version:
First, I'm going to give you the error explanation. After that, I'm including a particular case suitable for your needs.
That error means that your DocumentListener
is either an abstract class, or an interface (read about these two, then you'll get the idea).
The solution is either to look for subclasses of it (press CTRL + T while the class name is selected in the Eclipse IDE editor) OR to create an anonymous class right where you're instantiating it (see example below). Keep in mind that instantiating interfaces is generally not a good idea (not unless you know what you're doing), i.e. interfaces should be implemented by separate classes, and then instantiate THOSE classes.
DocumentListener dl = new DocumentListener()
{
// implement abstract methods here
};
Knowing this, we now have to particularise all of this to your code.
When attaching listeners to different elements (usually, listeners are interfaces), the code standard is to add the listener as an anonymous class directly as the API parameter, like so:
age.getDocument().addDocumentListener(new DocumentListener()
{
// implement necessary methods here
});
A quick Google Search instantly tells me what methods you need to implement there. Finally, your code should look like:
age.getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
}
public void insertUpdate(DocumentEvent e)
{
}
public void removeUpdate(DocumentEvent e)
{
}
});
Inside these methods, you may do whatever you want with those receiving events (of type DocumentEvent
- read the javadoc)
ANOTHER way to do it (the way that you've already started doing), is to implement the DocumentListener
directly in your working class, and then implement those methods there (as you did - notice the three overridden methods at the end of your class). In this case, all you have to do is tell the age.getDocument()
where it can find the listener.
age.getDocument().addDocumentListener(this);
By writing this
as listener, it means that when DocumentEvent
s are thrown, the execution will pass through those methods of yours there (changedUpdate
, insertUpdate
, removeUpdate
).
Upvotes: 1