Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Still wrestling with action listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Still wrestling with action listeners" Watch "Still wrestling with action listeners" New topic
Author

Still wrestling with action listeners

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Hello,

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.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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.
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
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.
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
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.
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

Hi
extends your AL class from JDialog and in the constractor write this

hope this work


http://www.myviwo.com
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

You mean setModal?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.

Hope that makes sense!
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

Hi Rob, setModal(true) is correct, tanx.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Howdy, y'all.

Jerry, were you able to solve your problem and clear your doubts?


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Still wrestling with action listeners