• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bringing my action listener in line  RSS feed

 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Main class:


SetString class:



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

Inside actionPerformed


EDIT by mw: Added Code Tags. Please use these in the future.
[ January 22, 2007: Message edited by: marc weber ]
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan.

Yes, that does make sense from a logical standpoint anyway. Let's see how successful I am at translating it into code.

Thanks especially for the idea of calling the second class from the action listener. I'll begin there.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us know if that gets you going. Be sure to come back, because there is a bit more voodoo around threads to talk about, but I don't want to make things more complex just now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!