Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

refer from within thread to the parent object  RSS feed

 
Mark Vermeersch
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

this is supposed to be easy but I just don't know how to do it. I have a class named XPanel, containing a "Slideshow" thread class. Inside the "run" method of the thread, i would like to use something like "this.getGraphics", with "this" referring to the XParent object. But as I am using a thread class inside another class, this doesn't work and I don't know how to refer to the XPanel object.



Any thoughts on this?
Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's see. If you're really using an inner class just as shown, what you describe should actually just work. An inner class can call the methods of the outer class directly. In those cases where doing so is ambigous, you can refer to the instance of the enclosing class as, e.g., "XPanel.this", as in "XPanel.this.getGraphics()".

But if this isn't working, then I suspect that you're using a static (nested) class, or no nested classes at all. In that case, the general solution would be to give your THread class a constructor that takes an XPanel as an argument, and stores that in a member variable; then the creating XPanel can pass "this" to the constructor, and the Thread subclass can refer to the member variable as needed.
 
Mark Vermeersch
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, this works. I suspected it would be that easy.
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!