I'm struggling with getting my action listener to execute when I want it to. Regardless of where I place the "addActionListener" in my code, it always seems to be called after all the other lines of code have been called. Here is some example code:
When I run the Main class I get the following results:
Top line of main method
Top line of method setString
Bottom line of method setString
Inside method printName
Bottom line of main method
EDIT by mw: Added Code Tags. Please use these in the future. [ January 22, 2007: Message edited by: marc weber ]
Sorry, I hit "Enter" too quickly. What I would really like is for the program to wait at the place in the code where addActionListener resides until actionPerformed is executed, and then pass control back to the setString method. The output should look something like this:
Top line of main method Top line of method setString Inside actionPerformed Bottom line of method setString Inside method printName Bottom line of main method
actionPerformed is not called as a result of calling addActionListener; you won't see the output you describe unless you actually press the button. When you call "component.addActionListener(myListener)", you're saying "Mr. Component, when somebody clicks you, presses 'Enter' on you, or something of that sort, please call myListener.actionPerformed()".
Now, other the important thing to know is that actionPerformed() is going to be called on the "AWT Event Thread", which is a different thread altogether from the "Main Thread" where your main() is invoked. Therefore what you're asking for actually involves communication between threads -- not a terribly hard thing to do or learn, but it does mess with your idea of ordered, linear execution, which is, I guess, what you've been trying to tell us all along.
I'll be happy to try to figure this out, but first, before we spend a lot of energy implementing something you don't really want, you have to explain to me the goal of this exercise -- what are you hoping to accomplish? We should do it the right way, whatever it is.
What I'm really trying to do is develop an independent GUI which solicits input from a user, via text fields in a panel, and passes that input to another class in the form of a string or strings. The second class will call methods from the JLink application within Pro/Engineer CAD software.
For example, the GUI will pass a part name to the CAD application which will then create a new part file using that name. Or, the GUI will pass a material name to the CAD application which will be automatically placed in a note in a drawing.
In addition to being a nice modular programming approach, the GUI needs to be independent because there is a conflict between the statement "import.java.awt.event.*;", which I need in the GUI, and the statement "import com.ptc.pfc.pfcBase.*;" which is required by the JLink application.
They have different methods with the same name. If I remember correctly it is WindowListener.
The event driven world is a bit different. It's more like you run some code to get the GUI displayed and then absolutely nothing happens. All the setup and display methods run through and exit method. None of your code is running or blocking while waiting or anything.
But, something deep inside of Swing is just waiting for a window event. When an event comes along Swing calls your listener. Then you get do make something happen. So the list of messages you showed first is just what we should expect ... assuming the user hit a button to make that action performed thing go.
The code in your action listener can call your second class which can call your CAD program.
Does that kinda makes sense?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi