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

JPanel autorefresh  RSS feed

 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Would anybody know of a way to call a specific method whenever a JPanel is about to be displayed? I will use this method to do some dataprocessing so that the JPanel would always have up-to-date data.

Thanks.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, but you might be able to get the same effect by the paintComponent method which is called only when the component is visible.
You might have to keep a track if you have processed your code, else it will get processed everytime paintComponent is called.
 
Suraj Chandran
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I beg to differ

I would suggest overriding addNotify in you panel. The method will be called as soon it becomes displayable.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch there Suraj
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just don't forget to call super.addNotify()!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a fan of subclassing if the desired behavior can be obtained in another way.


 
Suraj Chandran
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure? I think its not guaranteed.
Just make a small demo and check yourself.
The method componentShown() does not get called for a JPanel when it becomes visible.

Homework...find out why
(Hint: It does get called for a dialog and a frame )
 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, thanks for all suggestions. But I guess my question was really naive as I am not really a Swing developer. Anyway, the solution that I ended up doing is to have a listener class for a particular model class. When that model changes, it fires an event and the listener catches, which then refreshes the Panel
 
takatoshi yanase
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following URL that is a Java Applet is a sample of switching JPanels.

http://www.sys-audit.com/app1.html
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike the previous time you posted that link, I don't see the relevance of it this time. Is there any reason for posting it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!