CrimsonK9 CrimsonK9 - 23 days ago 6
Java Question

JButton can't be resolved inside actionListener

I'm trying to make a simple GUI calculator with Eclipse
The actionlistener is at the bottom, and eclipse says my ButtonAdd can't be resolved
the error is around lines 125-131, Any help is appreciated! :D

package main;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionListener;

public class GUIApp {

private JFrame frame;
public JTextField txtNumber_1;
public JTextField txtNumber;
public JTextField textField;
public JButton ButtonAdd;
public JButton ButtonSub;
public JButton ButtonMulti;
public JButton ButtonDiv;
public JButton btnRng;
String num1;
String num2;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUIApp window = new GUIApp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public GUIApp() {
initialize();
}

/**
* Initialize the contents of the frame.
* @return
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);

ButtonAdd = new JButton("Addition");
ButtonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
num1 = txtNumber_1.getText();
num2 = txtNumber.getText();
if(ButtonAdd.getModel().isPressed())
textField.setText(num1 + num2);
}
});
GridBagConstraints gbc_ButtonAdd = new GridBagConstraints();
gbc_ButtonAdd.insets = new Insets(0, 0, 5, 5);
gbc_ButtonAdd.gridx = 0;
gbc_ButtonAdd.gridy = 0;
frame.getContentPane().add(ButtonAdd, gbc_ButtonAdd);

ButtonSub = new JButton("Subtraction");
GridBagConstraints gbc_ButtonSub = new GridBagConstraints();
gbc_ButtonSub.insets = new Insets(0, 0, 5, 5);
gbc_ButtonSub.gridx = 0;
gbc_ButtonSub.gridy = 2;
frame.getContentPane().add(ButtonSub, gbc_ButtonSub);

txtNumber_1 = new JTextField();
txtNumber_1.setText("Number 1");
txtNumber_1.setToolTipText("insert a number");
GridBagConstraints gbc_txtNumber_1 = new GridBagConstraints();
gbc_txtNumber_1.insets = new Insets(0, 0, 5, 0);
gbc_txtNumber_1.fill = GridBagConstraints.HORIZONTAL;
gbc_txtNumber_1.gridx = 3;
gbc_txtNumber_1.gridy = 2;
frame.getContentPane().add(txtNumber_1, gbc_txtNumber_1);
txtNumber_1.setColumns(10);

ButtonMulti = new JButton("Multiplication");
GridBagConstraints gbc_ButtonMulti = new GridBagConstraints();
gbc_ButtonMulti.insets = new Insets(0, 0, 5, 5);
gbc_ButtonMulti.gridx = 0;
gbc_ButtonMulti.gridy = 4;
frame.getContentPane().add(ButtonMulti, gbc_ButtonMulti);

txtNumber = new JTextField();
txtNumber.setText("Number 2");
txtNumber.setToolTipText("insert other number");
GridBagConstraints gbc_txtNumber = new GridBagConstraints();
gbc_txtNumber.insets = new Insets(0, 0, 5, 0);
gbc_txtNumber.fill = GridBagConstraints.HORIZONTAL;
gbc_txtNumber.gridx = 3;
gbc_txtNumber.gridy = 5;
frame.getContentPane().add(txtNumber, gbc_txtNumber);
txtNumber.setColumns(10);

ButtonDiv = new JButton("Division");
GridBagConstraints gbc_ButtonDiv = new GridBagConstraints();
gbc_ButtonDiv.insets = new Insets(0, 0, 5, 5);
gbc_ButtonDiv.gridx = 0;
gbc_ButtonDiv.gridy = 6;
frame.getContentPane().add(ButtonDiv, gbc_ButtonDiv);

btnRng = new JButton("RNG");
GridBagConstraints gbc_btnRng = new GridBagConstraints();
gbc_btnRng.insets = new Insets(0, 0, 0, 5);
gbc_btnRng.gridx = 0;
gbc_btnRng.gridy = 8;
frame.getContentPane().add(btnRng, gbc_btnRng);

textField = new JTextField();
textField.setToolTipText("output");
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.gridx = 3;
gbc_textField.gridy = 8;
frame.getContentPane().add(textField, gbc_textField);
textField.setColumns(10);
}
public void actionPerformed(ActionEvent arg0) {
String num1;
String num2;
num1 = txtNumber_1.getText();
num2 = txtNumber.getText();
if(ButtonAdd.getModel().isPressed())
textField.setText(num1 + num2);

}
}


The GUI was made with WindowBuilder by the way

Answer

ButtonAdd is a local reference, meaning it can only be accessed inside the initialize method. A solution to this is to declare the ButtonAdd outside the method.

JButton ButtonAdd;

public void initialize() {
   ButtonAdd = new JButton("Addition");
}
public void actionPerformed(ActionEvent arg0) {
   // here we can now access ButtonAdd
}

Also a tip, by java's standard naming conventions you usually start variables and references with a lower case letter, so ButtonAdd -> buttonAdd