• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call repaint() from another class?  RSS feed

 
miroslav mits
Greenhorn
Posts: 3
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code I need to call repaint() from another class, is there any way to do it?



---------


 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
repaint() is a method that belongs to a class.  You call a class's method by using a reference to an instance of the class.
The problem then is to have a reference to the class available so its methods can be called.  One way is to pass the reference as a parameter to a constructor or method.
 
Mohammed Sardar.
Ranch Hand
Posts: 193
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
miroslav mits wrote:In my code I need to call repaint() from another class, is there any way to do it?



https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html

Repaint is predefined method that we can use to repaint a component.Please have at above link to proceed.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your posted code, what's the relationship between Example1 and Example2?  Does Example1 have a reference to Example2 or vice versa?
 
miroslav mits
Greenhorn
Posts: 3
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:In your posted code, what's the relationship between Example1 and Example2?  Does Example1 have a reference to Example2 or vice versa?


No, but example1 would(in my real code) have a reference to example2
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your other class, presumably you have an instance of MyDrawPanel (let's call it myPanel);
then you just call
(Make sure that is done from the Event Dispatch thread.)

Incidentally, the first statement in your paintCompoinent() method should be

If you don't do that, the panel's borders and background may not end up rendered properly.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example1 would(in my real code) have a reference to example2

Okay, then what I'd do is use constructor injection (I believe that's the name for it).

First, create an instance member of type MyDrawPanel in Example2.  Then instantiate that member via a constructor.  Now that you have a reference to MyDrawPanel, you can use it in your paint() method.

To call this paint() method, create an instance of Example2 in Example1, passing in the MyDrawPanel object reference.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!