• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting the object that created this object

 
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a object which needs to change a variable of an object that created it - is there any way for the object to find out which object created it (and hence give me a handle on the variable)?

Thanks.
Adam
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only if the object keeps a reference to its creator.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Cripps wrote:I have a object which needs to change a variable of an object that created it -



That sounds fishy. Why would an object care which object created it? And what if no object created it? Or what if the object that created it doesn't have that variable?

Can you explain why you want to do this?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adam

It sounds like you need to redesign your model here. Can you provide more information on your code base please?
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JFrame which holds an JTextArea. Also, inside the JFrame is a TabbedPane - I created new instances of the TabbedPane so that I could handle new ones in the future if they come up.

The TabbedPanes each have JTextFields, which I want to getText from and then insert that in to the JTextArea.

I have fixed this - when I created an instance of TabbedPane, I passed the JFrame instance and then created a class variable instance of this so that actionPerformed could get a hold of it.

You can now mark this as a non-issue.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why didn’t you use the methods to get a parent or ancestor object?
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@CampbellRitchie - I didn't use them because I don't know about them - can you give me a pointer? Thanks.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.


Oh, all right then, click the no and you should find something useful. Then scroll up to the method summary and you can find other methods.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

I though that applied to bulls.
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link - I did see this link but was confused as to whether it would return the object that instantiated the class that calls the getParent, or whether it returned the super class. I will investigate.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Cripps wrote:Thanks for the link - I did see this link but was confused as to whether it would return the object that instantiated the class that calls the getParent, or whether it returned the super class. I will investigate.



It doesn't return either of those things. It returns the Swing component which contains the component which calls the method. For example if you add a JLabel to a JPanel, then calling the JLabel's getParent() method returns a reference to that JPanel. That's what "parent" means in the Swing component hierarchy.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: It returns the Swing component which contains the component which calls the method. For example if you add a JLabel to a JPanel, then calling the JLabel's getParent() method returns a reference to that JPanel. That's what "parent" means in the Swing component hierarchy.



In particular, in case it's not clear, "parent/child" in Swing's component hierarchy has nothing to do with "parent/child" in a class relationship. In the context of Swing components being discussed here, it's about which GUI element manages or owns or contains another for things like events and window management, and they will in general not be in a parent/child class relationship.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Bear Bibeault wrote:

I though that applied to bulls.


And to code smells.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get at methods from the superclass with the super keyword.

You should not call superclasses parents. A parent-child relationship is a GUI term as Jeff Verdegan has told you. It means the parent object surrounds the child object on the display.
 
reply
    Bookmark Topic Watch Topic
  • New Topic