I would like to pass a variable set in an action listener back to the Main class. I have downloaded and experimented with a number of examples including DialogDemo.java and AL.java. I can get the value to display from within the action listener; but, the code in the Main class always executes, before the value is returned from the action listener, resulting in a null value. Can anyone tell me what I'm missing? Please see below for an example. Thanks in advance.
What you are missing is that you are creating a new thread. You can see that by adding the following line as the last line in the Main#main methodThat will close the main thread, but the thread the display runs on continues running. [You should actually use a different technique for starting a Swing application, which you can find here.] So the main method blocks until you click OK on the JOptionPane (it is a "modal" dialogue window), and thereafter the main method completes. You can see that when you get the Exception. But the display runs on.
I don't think it is possible to get such information back to the main method. Nor would it be desirable. The idea behind a GUI application is that it runs on its own once it is started; you don't want to be going back to the command line.
By the way: I don't like the advice you have been given to make the Frame implement ActionListener. Never mind that it is in many books; once you get lots of controls all using an ActionEvent, your actionPerformed method becomes bloated and difficult to maintain.
Also you have found some very old-fashioned code if you are using a WindowListener simply to close the frame. Look at the JFrame documentation and you find there may be a better way to close the frame. A WindowListener is useful if (for example) you want to save files when you close the frame, in which case System.exit(0) is far too vicious, and may terminate the JVM before all the files are saved.
Joined: Nov 29, 2006
Thanks for the reply. I am, however, no closer to my goal of writing a simple method, which inputs an array of choices, displays a set of radio buttons, and outputs the selection. This seems fairly elementary but I've read and experimented with a number of demos, googled the issue, and posted here several times with no result. This can't be that difficult. I'm actually surpised that there isn't already a class out there that does this.
Joined: Nov 29, 2006
It sounds like I need to nest action listeners within action listeners within action listeners. This seems kind of clunky but everything would execute from a single GUI app. I believe the DialogDemo.java example has at least one level of nested action listeners.
There are also some custom menu calls from within the app I'm customizing. I might be better off going with those rather than using Swing.
OP, I'm pretty clueless on this myself, but I think what you want to do is to create a JOptionPane as an instance variable of your AL class. Instantiate it and add is as a listener to the button in the AL constructor, rather than in your main method. As it stands, you're asking to show the number of clicks, but after how long? Straight away, it'll always be zero, but you need it to dynamically react to clicks.
You need to create a ClickListener as an inner class, where it'll have scope on the option pane, and use that inner class's actionPerformed to update the number displayed.
Also suspect a JOptionPane isn't the right vehicle for displaying the number of clicks, unless you want a new one to spring up each time you click, because I don't think you can update the value displayed on it.
Or, after actually reading your post, if you want is an option pane to spring up when the user closes the window, simply create the new option pane in the windowClosing method (or its modern-day counterpart) rather than your main method.