Swing and threads

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 2
Reputation: renoss is an unknown quantity at this point 
Solved Threads: 0
renoss renoss is offline Offline
Newbie Poster

Swing and threads

 
0
  #1
Sep 24th, 2008
Hi,
My problem is that I need to tell class A to repaint, but from class B.
Class B will be a thread that updates components of class A and will eventually tell A to repaint.
The basic idea of my code is as follows:

  1. class A extends JPanel{
  2. Thread b;
  3. ...
  4. b = new B();
  5.  
  6. paint(Graphics g){
  7. ...
  8. }
  9. }
  10.  
  11. class B extends A implements Runnable{
  12. run(){
  13. ...
  14. //Tell A to repaint
  15. }
  16. }

From my understanding of swing, I need to tell A to repaint when referring directly to it
For example:
  1. class X{
  2. A a;
  3. ...
  4. a.repaint();
  5. }

Or I could call repaint inside of Class A
  1. class A{
  2. ...
  3. repaint();
  4. }

But how do I do this from the run() method of class B?

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Swing and threads

 
0
  #2
Sep 24th, 2008
Why exactly does class B have to extend A?

From an immediate glance I can see a potential cycle instantiation issue.

Think about it--

1) Class A, after static initialization, will instantiate a class B object--

2) Class B extends A, so when a B object is constructed, the super constructor of A is called but between that process the pre-initialization is done again leading back to step 1!

--which means that your program will cause new memory to be called and constructors to be (in theory) pushed on stack memory along with variables that are used within them, and additional growth of objects in the heap due to subsequent calls of new that are not resolved to make objects unreachable since there is a valid reference pointing to it.

What you want to do is something like this--

  1. import javax.swing.*;
  2.  
  3. public class NameSpaceClass001{
  4.  
  5. class A extends JPanel{
  6.  
  7. // blah... mumble...
  8. private B myB = null;
  9. public A(){
  10. myB = new B(this);
  11. Thread t = new Thread(myB);
  12. t.setDaemon(true);
  13. t.start();
  14. }
  15. }
  16.  
  17. class B implements Runnable{
  18.  
  19. private A panel = null;
  20. public boolean running = true;
  21.  
  22. public B(A ref){
  23. panel = ref;
  24. }
  25.  
  26. private int safeWait(int seconds){
  27. long l = System.currentTimeMillis(); // get current time
  28. long next = l + (seconds * 1000); // get current time + future time
  29. while(l < next) l = System.currentTimeMillis(); // wait specified seconds
  30. return seconds;
  31. }
  32.  
  33. @Override public void run(){
  34. while(running){
  35. System.out.println("Calling repaint after " + safeWait(3) + " seconds...");
  36. panel.repaint();
  37.  
  38. }
  39. }
  40. }
  41.  
  42. public static void main(String... args){
  43. NameSpaceClass001 nsc001 = new NameSpaceClass001();
  44. nsc001.new A();
  45. }
  46. }
Last edited by Alex Edwards; Sep 24th, 2008 at 3:00 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Swing and threads

 
1
  #3
Sep 24th, 2008
I marked the Thread daemon as a good practice. Unfortunately it makes the example hard to see, the way the code is currently written. Consider this example instead--

  1. import javax.swing.*;
  2.  
  3. public class NameSpaceClass001{
  4.  
  5. class A extends JPanel{
  6.  
  7. // blah... mumble...
  8. private B myB = null;
  9. public A(){
  10. myB = new B(this);
  11. Thread t = new Thread(myB);
  12. t.setDaemon(true);
  13. t.start();
  14. }
  15.  
  16. public B getB(){
  17. return myB;
  18. }
  19. }
  20.  
  21. class B implements Runnable{
  22.  
  23. private A panel = null;
  24. public boolean running = true;
  25.  
  26. public B(A ref){
  27. panel = ref;
  28. }
  29.  
  30. public int safeWait(int seconds){
  31. long l = System.currentTimeMillis(); // get current time
  32. long next = l + (seconds * 1000); // get current time + future time
  33. while(l < next) l = System.currentTimeMillis(); // wait specified seconds
  34. return seconds;
  35. }
  36.  
  37. @Override public void run(){
  38. while(running){
  39. System.out.println("Calling repaint after " + safeWait(3) + " seconds...");
  40. panel.repaint();
  41.  
  42. }
  43. }
  44. }
  45.  
  46. public static void main(String... args){
  47. NameSpaceClass001 nsc001 = new NameSpaceClass001();
  48. NameSpaceClass001.A myA = nsc001.new A();
  49.  
  50. myA.getB().safeWait(15);
  51. }
  52. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: renoss is an unknown quantity at this point 
Solved Threads: 0
renoss renoss is offline Offline
Newbie Poster

Re: Swing and threads

 
0
  #4
Sep 24th, 2008
Thankyou that was the problem, plenty of good karma is coming your way...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Swing and threads

 
1
  #5
Sep 24th, 2008
Keep in mind that updates to Swing components should be done on the AWT event queue.
http://java.sun.com/docs/books/tutor...ncy/index.html
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 505 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC