• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Getting the object that created this object

 
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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: 22042
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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
  • 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: 71094
292
  • Mark post as helpful
  • send pies
  • 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
  • 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: 71094
292
  • Mark post as helpful
  • send pies
  • 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: 71094
292
  • Mark post as helpful
  • send pies
  • 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
  • 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: 26135
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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: 71094
292
  • Mark post as helpful
  • send pies
  • 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.
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic