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!
}
}
}
}