ActionListener from button for JTextField, JTextField cannot be Resolved

248 views Asked by At

I'm trying to get a head start on something I will be working on next semester. Its basically a template of a cell phone, consisting of a JTextField that displays the buttons pressed. My problem is when making my actionlistener, the JTextField (named "numIn") is not being recognized, getting an error saying it cannot be resolved. Here is the code for the way I have my JPanel of the phone set up:

public class Template
{
    // the dial pad button strings
    private static final String[][] BUTTONSTRINGS =
    {
        {"1", "2", "3"},
        {"4", "5", "6"},
        {"7", "8", "9"},
        {"*", "0", "#"}
    };

    private static final Dimension JUNK_SIZE = new Dimension(200, 160);
    private JPanel mainPanel = new JPanel();

    public Template()
    {
        JTextField numIn = new JTextField("Enter Phone Number");
        JTextField numDisplay = new JTextField("PhoneNumber");
        JPanel otherJunkPanel = new JPanel();
        otherJunkPanel.add(numDisplay);
        otherJunkPanel.add(numIn);
        otherJunkPanel.add(new JButton("Send"));
        otherJunkPanel.setPreferredSize(JUNK_SIZE);

        JPanel dialPadPanel = new JPanel(new GridLayout(0, 3));

Here is the two action listeners made, one for the numbers, one for the non numbers:

// action listener for the number buttons only 
NumberButtonListener numberBtnListener = new NumberButtonListener();
// listener for other buttons
NonNumberButtonListener nonNumberBtnListener = new NonNumberButtonListener();
for (int i = 0; i < BUTTONSTRINGS.length; i++)
{
    for (int j = 0; j < BUTTONSTRINGS[i].length; j++)
    {
        String btnString = BUTTONSTRINGS[i][j]; // get the button string from array
        JButton btn = new JButton(btnString); // use it to make button

        // if a number button, add the number button's listener
        if ("012345679".contains(btnString))
        {
            btn.addActionListener(numberBtnListener);
        }
        else
        {
            btn.addActionListener(nonNumberBtnListener);
        }

and here is where i get my error, when i tell the action listener to display the button at the JTextField named "numIn", numIn is not recognized:

private class NumberButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {   
        String buttonPressedString = e.getActionCommand();
        numIn.setText("Number Button Pressed: " + buttonPressedString);
        // TODO finish      
    }
}

I am not very strong in coding, as its not my major, so I appreciate any help possible. I should have probably prefaced that much of this code has been frankenstiend from bits and pieces of labs I have done in the past. Thanks!

3

There are 3 answers

0
Marcos Vasconcelos On

Your numIn is a local variable on the constructor.

You should declare it as a class variable if you want to use it on other methods. (or pass it by argument for methods)

0
subash On
    JTextField numIn = new JTextField("Enter Phone Number");
    JTextField numDisplay = new JTextField("PhoneNumber");
    public Template()
    {
       .......

you need to put that in class level scope. currently it is in constructor. so it will not available for out side of the constructor

1
AudioBubble On

Try this

private class NumberButtonListener implements ActionListener
 {
 Template parent=null;
 NumberButtonListener(Template parent)
 {
    this.parent = parent;
 }

  public void actionPerformed(ActionEvent e)
  {

    String buttonPressedString = e.getActionCommand();
     parent.numIn.setText("Number Button Pressed: " + buttonPressedString);
    // TODO finish      
  }
 }

Then change this

 NumberButtonListener numberBtnListener = new NumberButtonListener();

to

 NumberButtonListener numberBtnListener = new NumberButtonListener(this);

Add a new private field to your Template class

  private JTextField numIn=null;

In the constructor change to this

  numIn = new JTextField("Enter Phone Number");