Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the object that created this object

 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if the object keeps a reference to its creator.
 
Jeff Verdegan
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 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 Linux Ubuntu
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • 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 Linux Ubuntu
  • 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
Sheriff
Posts: 48921
58
  • 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
Sheriff
Posts: 48921
58
  • 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 Linux Ubuntu
  • 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.
 
Paul Clapham
Sheriff
Posts: 21107
32
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
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
Sheriff
Posts: 48921
58
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic