Reputation: 21
I guess the errors mentioned below belongs to something about the scope of variables while accessing them in another class. I would appreciate your help resolving this.
class Main {
public static void main(String[] args) {
JFrame frame = new CurrencyConversionFrame();
}
}
public class CurrencyConversionFrame extends JFrame {
private JTextField currencyAmountField;
private JButton createConvertButton() {
JButton button = new JButton("Convert");
ActionListener listener = new ConvertListener();
button.addActionListener(listener);
return button;
}
}
class ConvertListener implements ActionListener {
public void actionPerformed(ActionEvent action) {
double balance = 0;
try {
balance = Double.parseDouble(currencyAmountField.getText());
}
}
}
The code provided above contains snippets from the original lengthy code. I have three classes "ConvertListener", "CurrencyConversionFrame" and one main class "Main". When I run the code I only get the following errors for all the variables declared in "CurrencyConversionFrame" class.
ConvertListener.java:9: error: cannot find symbol
balance = Double.parseDouble(currencyAmountField.getText());
^
symbol: variable currencyAmountField
location: class ConvertListener
Upvotes: 0
Views: 61
Reputation: 16354
The event source items is accessed through the ActionEvent#getSource
method and you should not need to access the origin field:
class ConvertListener implements ActionListener {
public void actionPerformed(ActionEvent action) {
double balance = 0;
try {
Object source = action.getSource(); // here you have the source button and you can deal with its content
}
}
}
When dealing with UI component generally in Swing / JavaFX world, event listeners are encapsulated within their dependent component (since they are generally tied to the latter), hence they get access to the outer
type fields.
If you then still need to access the underlying field directly, which is discouraged, you can then move your ConvertListener
to be an inner type or your UI component.
Upvotes: 0
Reputation: 1527
In order to access a class fields (conventionally) you should declare getters and setters so in your case it would be
public void setCurrencyAmountField(JTextField currencyAmountField) {
this.currencyAmountField = currencyAmountField;
}
public JTextField getCurrencyAmountField() {
return this.currencyAmountField;
}
This way accessing the variable is doable through the getCurrencyAmountField();
Also one thing that might help is to implement the ActionListener directly into the frame and determine through a switch statement what type of action was actually performed (if the frame has more than one possibility)
public class CurrencyConversionFrame extends JFrame implements ActionListener {
...
public void actionPerformed(ActionEvent action) {
switch(action.getActionCommand()) {
case "Name Of the button":
balance = Double.parseDouble(currencyAmountField.getText());
break;
default:
System.out.println("The action is not yet defined");
}
}
}
Upvotes: 1