Paul Clapham wrote:
Dale Viljoen wrote:But how do I get the method to "wait" for the user to trigger the action listener and return the value back to the introduction() method? Or is that not possible?
It's not possible, and it's not meaningful to ask that anyway. In a Swing application, after you construct the GUI and call its setVisible(true) method, Swing is in charge. It waits for the user to do things to the GUI, like typing in text fields and clicking on buttons. When the user does certain things, Swing calls your action listeners and other similar methods. The purpose of your code is simply to react to those events.
Paul Clapham wrote:Well, for your example I don't see why you need two separate classes. There's no reason to have a class which extends JPanel; the class you posted doesn't override JPanel in any way. So you could perfectly well include all of its code in your GUI class. Which, by the way, doesn't override JFrame in any way so it shouldn't extend JFrame either. Just create a JFrame in your GUI class and work with it.
After you do that you'll find that you only have one class and your problem goes away.
There are certainly ways to communicate between two objects (not "between two classes" as you think) but it's better to avoid that until you've studied object-oriented design a bit more.
Junilu Lacar wrote:You don't. It depends on how you're asking the user. If it's via a modal dialog, like what this tutorial about creating dialogs shows, you'll get the answer as a return value. If the user has to enter the answer into a text field and then click a "Submit" button, then you'd have an ActionListener on the submit button and then get the value of the answer from the text field.