• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing a String from a called objects ActionListener back to the original calling object.

 
Morrie Segal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java (about three weeks). I have two classes residing in two separate files. The first class "news" the second class. The second class has a JFileChooser in it. When the JFileChooser's ActionListener wakes up I want it to get the path of the chosen file and SOMEHOW get it back to the 1st class. I can get the path, no problem. I can place it on the screen, no problem. I just can't figure out how to get it back to the first class for additional processing. This should be easy, but I have been grinding my brain on it all day and I give up. The ActionListener just doesn't seem to be in a position to interface with the first class. Any ideas? Can the second class invoke a setter routine in the first class? What does he call the first classes object, the one that called him?

class1 news class2
class2's ActionListener wakes up and gets file path
now what? How does the ActionListener get the path string back to the first class?
 
Rob Camick
Ranch Hand
Posts: 2699
10
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are talking about passing a String from an actionPerformed method back to the calling object? If you can't figure out a way to actually "pass" the string back to the caller, then there should be a way to have the actionPerformed method jut set a variable, and have the calling object "retrieve" the value.
 
Morrie Segal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't even k ow enough to state that I have asked this to the Sun Forum also. I have lots to learn.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you be more specific about "When the JFileChooser's ActionListener wakes up..." maybe by showing us the code that does this? I'm unclear on how one class is calling the other in your scenario.
 
Morrie Segal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:So you are talking about passing a String from an actionPerformed method back to the calling object? If you can't figure out a way to actually "pass" the string back to the caller, then there should be a way to have the actionPerformed method jut set a variable, and have the calling object "retrieve" the value.


I guess was planning on driving the ActionEvent from the second class because it knows when the event occurs. I would need a listener on the first class to wake up when the second class gets notified. Right?
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morrie Segal wrote:
Fred Hamilton wrote:So you are talking about passing a String from an actionPerformed method back to the calling object? If you can't figure out a way to actually "pass" the string back to the caller, then there should be a way to have the actionPerformed method jut set a variable, and have the calling object "retrieve" the value.


I guess was planning on driving the ActionEvent from the second class because it knows when the event occurs. I would need a listener on the first class to wake up when the second class gets notified. Right?


OK, I think I see what you mean now. I think. I was somehow mislead by your use of the term "calling object" in the subject. OK so the first object, of which the second object is really just an instance variable, needs to wake up. I ddn't understand that at first.

I'm don't fully understand your problem, but maybe you can accomplish what you want by making the second class an inner class within the first class. That way instance variables of the first class will be available to the action handler of the second class. Just a thought. Maybe the following link will help. Good luck with it.

http://www.javaworld.com/javaworld/javatips/jw-javatip75.html

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't even k ow enough to state that I have asked this to the Sun Forum also.


Well, know that you know why haven't you replied to the other posting stating it was cross posted?
 
Morrie Segal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Sorry, I didn't even k ow enough to state that I have asked this to the Sun Forum also.


Well, know that you know why haven't you replied to the other posting stating it was cross posted?


Sorry - deadline, been on this all day and just can't seem to make you happy. I'll go post it now.
 
Morrie Segal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:
Morrie Segal wrote:
Fred Hamilton wrote:So you are talking about passing a String from an actionPerformed method back to the calling object? If you can't figure out a way to actually "pass" the string back to the caller, then there should be a way to have the actionPerformed method jut set a variable, and have the calling object "retrieve" the value.


I guess was planning on driving the ActionEvent from the second class because it knows when the event occurs. I would need a listener on the first class to wake up when the second class gets notified. Right?


OK, I think I see what you mean now. I think. I was somehow mislead by your use of the term "calling object" in the subject. OK so the first object, of which the second object is really just an instance variable, needs to wake up. I ddn't understand that at first.

I'm don't fully understand your problem, but maybe you can accomplish what you want by making the second class an inner class within the first class. That way instance variables of the first class will be available to the action handler of the second class. Just a thought. Maybe the following link will help. Good luck with it.

http://www.javaworld.com/javaworld/javatips/jw-javatip75.html

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html



Thanks for the links and ideas. I will give the inner class a try. Thanks again!
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morrie Segal wrote:
Fred Hamilton wrote:
Morrie Segal wrote:
Fred Hamilton wrote:So you are talking about passing a String from an actionPerformed method back to the calling object? If you can't figure out a way to actually "pass" the string back to the caller, then there should be a way to have the actionPerformed method jut set a variable, and have the calling object "retrieve" the value.


I guess was planning on driving the ActionEvent from the second class because it knows when the event occurs. I would need a listener on the first class to wake up when the second class gets notified. Right?


OK, I think I see what you mean now. I think. I was somehow mislead by your use of the term "calling object" in the subject. OK so the first object, of which the second object is really just an instance variable, needs to wake up. I ddn't understand that at first.

I'm don't fully understand your problem, but maybe you can accomplish what you want by making the second class an inner class within the first class. That way instance variables of the first class will be available to the action handler of the second class. Just a thought. Maybe the following link will help. Good luck with it.

http://www.javaworld.com/javaworld/javatips/jw-javatip75.html

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html



Thanks for the links and ideas. I will give the inner class a try. Thanks again!


cool. Just one final point. As I see it, by using an inner class, I believe that will eliminate the need to "wake up" the first class, since it all happens "within" the first class anyway. Anyways, keep in mind this is all a bit of a shot in the dark, as I had to more or less guess at what you were trying to do. Let us know if this works for you.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also keep in mind that we are all just guessing here, which is why I asked to see some code. We really have no clue what you are doing and any suggestions are pure crystal ball guess work at this point.

 
Morrie Segal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Bolinger wrote:Also keep in mind that we are all just guessing here, which is why I asked to see some code. We really have no clue what you are doing and any suggestions are pure crystal ball guess work at this point.



Yes, I agree. There was just a lot of code in different files and I was not very proud of any of it. I usually try to build a test case that just shows the issue, but I didn't have time. Anyhow, it looks like the inner class suggestion is working. I still have a minor bug, but I'll figure that out on my own. Thanks to all of you I will close this out now. Thanks again!
 
Morrie Segal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:
Morrie Segal wrote:
Fred Hamilton wrote:
Morrie Segal wrote:
Fred Hamilton wrote:So you are talking about passing a String from an actionPerformed method back to the calling object? If you can't figure out a way to actually "pass" the string back to the caller, then there should be a way to have the actionPerformed method jut set a variable, and have the calling object "retrieve" the value.


I guess was planning on driving the ActionEvent from the second class because it knows when the event occurs. I would need a listener on the first class to wake up when the second class gets notified. Right?


OK, I think I see what you mean now. I think. I was somehow mislead by your use of the term "calling object" in the subject. OK so the first object, of which the second object is really just an instance variable, needs to wake up. I ddn't understand that at first.

I'm don't fully understand your problem, but maybe you can accomplish what you want by making the second class an inner class within the first class. That way instance variables of the first class will be available to the action handler of the second class. Just a thought. Maybe the following link will help. Good luck with it.

http://www.javaworld.com/javaworld/javatips/jw-javatip75.html

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html



Thanks for the links and ideas. I will give the inner class a try. Thanks again!


cool. Just one final point. As I see it, by using an inner class, I believe that will eliminate the need to "wake up" the first class, since it all happens "within" the first class anyway. Anyways, keep in mind this is all a bit of a shot in the dark, as I had to more or less guess at what you were trying to do. Let us know if this works for you.


The inner class is working Thanks again!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyhow, it looks like the inner class suggestion is working

That's great but quite possibly the wrong approach from what I have gleaned thus far.

I am new to java (about three weeks).

Come back to this thread when you aren't so busy and maybe give us the chance to help you learn a better approach to your problem. ;)
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would tend to agree with Gregg. Not so much that inner class is a bad solution based on the limitations of program design that have been imposed, but if we could have addressed the high level requirements of the problem, then there are probably better designs. The first thing I would have asked is whether you are not better served by making the file chooser and event handler part of class A, which sounds like a higher level class, and passing information to B, instead of the other way around as you have it.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:I would tend to agree with Gregg. Not so much that inner class is a bad solution based on the limitations of program design that have been imposed, but if we could have addressed the high level requirements of the problem, then there are probably better designs. The first thing I would have asked is whether you are not better served by making the file chooser and event handler part of class A, which sounds like a higher level class, and passing information to B, instead of the other way around as you have it.


+1
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!