Hi there, I'm having a weird error, When I click on my submit button my eventhandler
that should be capturing the data from my JTextField is throwing an error :

This is my first week using Java so I'm sorry if this is a newb thing.

My Main function simple instantiates a object of the Recipe class, that is all... basically the constructor of the Recipe class does all the work for now.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class Recipe extends JFrame{

    private JTextField diff;
    private JTextField prep;
    private JTextField name;
    private JTextArea wholeRecipe;
    private JButton submit;

        public Recipe(){
            super("The title");
            JFrame frame = new JFrame("test");
            frame.setSize(999, 400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel(new GridBagLayout());
            frame.getContentPane().add(panel, BorderLayout.NORTH);
            GridBagConstraints c = new GridBagConstraints();
            JLabel label0 = new JLabel(""); // adds padding to top
            c.gridx = 0;
            c.gridy = 0;
            label0.setPreferredSize(new Dimension(20, 20));
            c.insets = new Insets(0,10,10,0);
            panel.add(label0, c);

            JLabel label1 = new JLabel("Add Recipe Dialog Box");
            c.gridx = 0;
            c.gridy = 1;
            label1.setFont(new Font("Helvetica", Font.CENTER_BASELINE, 18));
            label1.setPreferredSize(new Dimension(300,20));
            panel.add(label1, c);

            JTextField diff = new JTextField();
            diff.setText("alba");
            c.gridx = 3;
            c.gridy = 1;
            diff.setPreferredSize(new Dimension(200,20));
            panel.add(diff, c);

            JTextField prep = new JTextField("Prep time in minutes");
            c.gridx = 4;
            c.gridy = 1;
            prep.setPreferredSize(new Dimension(200,20));
            panel.add(prep, c);

            JTextField name = new JTextField("Name of Recipe");
            c.gridx = 0;
            c.gridy = 2;
            c.gridwidth = 600;
            name.setPreferredSize(new Dimension(720,20));
            panel.add(name, c);

            JTextArea wholeRecipe = new JTextArea("\n Enter your recipe here.", 10, 5);
            c.gridx = 0;
            c.gridy = 3;
            c.gridwidth = 720;
            wholeRecipe.setPreferredSize(new Dimension(720, 40));
            panel.add(wholeRecipe, c);

            submit = new JButton("Submit Recipe");
            c.gridx = 4;
            c.gridy = 4;
            c.insets = new Insets(0,100,20,20);
            c.gridwidth = 500;
            panel.add(submit, c);

            frame.setSize(1000,400);

            TheHandler handler = new TheHandler();
            diff.addActionListener(handler);
            submit.addActionListener(handler);
            diff.setText("gghg");


        }

        private class TheHandler implements ActionListener{ // event handling class with ActionListener 
            public void actionPerformed(ActionEvent event){ // built in method that has to do with actionListener
                String string = "";
                if(event.getSource() == submit)
                {

                    wholeRecipe.getInputContext();  // THIS THROWS AN ERROR!
                }


            }
        }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

What's the purpose of getInputContext()? Do you know what is returned? I read its api and not sure why you did it that way?

What's the purpose of getInputContext()? Do you know what is returned? I read its api and not sure why you did it that way?

Oops... it's supposed to read getText(); I was just attempting other methods.

Hmm... I didn't realize that your code is broken. You are not supposed to access local variable inside an inner class. You will see the error when you attempt to implement a new ActionListener inside your addActionListener. Not sure about how to fix it but still keep your purpose of the program. Not have the head to work on it right now. :(

Edited 6 Years Ago by Taywin: n/a

If that is true why am I able to replace wholeRecipe.getText()

with submit.setText("some text") ?

Because it is only in your actionPerformed... I guess you have to ask Java... If you want to see the error, do the following...

// line 86
submit.addActionListener(handler);

// change it to...
submit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event){
      if (e.getSource()==submit) {  // can access submit because it is read only
        wholeRecipe.getInputContext();  // this is where you are not allowed
      }
    } 
  }
)
This article has been dead for over six months. Start a new discussion instead.