aspose file tools*
The moose likes Beginning Java and the fly likes ? member access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "? member access" Watch "? member access" New topic
Author

? member access

Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Why don't I get output to textField from my actionListener?

The file with interface and listener



The File with method in question, called from interface actionListener.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Your SetText object creates a new GetText object, with its own new text field. You probably want to give it a reference to the existing GetText object in the constructor.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I think it has to do with the fact that you are creating 2 GetText objects. Walk through the code.... you are creating the first one in the createAndShowGUI method which is then added to the ContentPane and then set to visible.... but what about the call to SetTextHere.outPut() method? It is also creating a new GetText object. Maybe something different should go here?

SCJA
~Currently preparing for SCJP6
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Sniped by Rob... again
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102

Your SetText object creates a new GetText object, with its own new text field. You probably want to give it a reference to the existing GetText object in the constructor.


By existing object, are you talking about the one named newContentPane?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Yes. You can reference it inside your action listener using "GetText.this" - you need the "GetText" prefix because "this" refers to your action listener.
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102

Yes. You can reference it inside your action listener using "GetText.this" - you need the "GetText" prefix because "this" refers to your action listener.




I understand what you are saying. But, the purpose of this exercise is to refer to the textField from another class. I am
having issues with 'fully qualified names', 'qualified names' ect. I'm reading up on the "Java Tutorial" at the moment.

After reading, If I still don't understand I'll post another reply to this thread.

Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Ok. According to the tutorial,

"You can use a package member's simple name if the code you are writing is in the same package as that member or if that member has been imported."

So, shouldn't I be able to reference the textField like this?



... where the textField being referred to is in the following file.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
Bad design, having public fields.
You cannot simply call the name of a field outside its class; the field belongs to an object, so you would have to use the name of the object-dot-name of the field.
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
which object?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
You will have to find an object to pass from the GetText class to the SetText here class. If you are going to do it that way, why not pass a reference to the text field directly?
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102

You will have to find an object to pass from the GetText class to the SetText here class.


This seems to be the big issue here for me. If I understand this correctly, the object that contains the member "textFieldOne"
is "newContentPane". I can't pass it as an argument because it is local to createAndShowGUI.



If you are going to do it that way, why not pass a reference to the text field directly?


Do you mean something like:


Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Yes, you can change your outPut method to accept a JTextField and then pass it in like you just said... Or

You can make your JTextField private, create a getter method for it, and then pass outPut method a reference to your GetText object which then could access the JTextField as needed.

Rob knows more GUI stuff then me but I think the second method is the "correct" way to handle this.

edit: grammar
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Actually now that I looked at your code example again... instead of this:



You don't need to pass the JTextField into your constructor unless you want SetText to keep a permanent reference to it. I would change it to this:



Your design and how these objects will interact will determine which method makes more sense.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
Geoff Jefferson wrote:. . . Do you mean something like:

Yes, but other people have refined the answer
 
Consider Paul's rocket mass heater.
 
subject: ? member access