• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

? member access

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sniped by Rob... again
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which object?
 
Campbell Ritchie
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geoff Jefferson wrote:. . . Do you mean something like:

Yes, but other people have refined the answer
reply
    Bookmark Topic Watch Topic
  • New Topic