3

I have two classes. One creates the GUI in JApplet, and the other class takes user input from the applet and does calculations with it.

The applet class creates GUI and tells the calculation class to start. Then the calculation class calls on a method from the applet class to ask for user input. This method then listens for action from a JTextField to return to the calculation class.

But the problem is that when you run the program, it doesn't actually wait for the user to enter something in the text field. As a result the calculation class receives a null input.

How can I fix this?

I've tried using a JOptionPane.showInputDialog instead of an ActionListener in the method, and this works. But I don't want to use this; mainly since it's like a pop-up and I want the user to input data directly in the applet.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
GreenBeans
  • 33
  • 1
  • 4
  • BTW 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) +1 for (re. `JOptionPane`) *"But I don't want to use this; mainly since it's like a pop-up.."* – Andrew Thompson Oct 28 '11 at 08:11

1 Answers1

5

Add an ActionListener to the JTextField in the applet. When an event is fired (typically when the user presses 'Enter'), call the calculation class using the String in the text field.

Or to put that a different way. The class that is the source of the event (the applet) should invoke the method of the calculation class. It should not be the other way around.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433