| | |
Swing and threads
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 2
Reputation:
Solved Threads: 0
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:
From my understanding of swing, I need to tell A to repaint when referring directly to it
For example:
Or I could call repaint inside of Class A
But how do I do this from the
Thanks in advance.
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:
Java Syntax (Toggle Plain Text)
class A extends JPanel{ Thread b; ... b = new B(); paint(Graphics g){ ... } } class B extends A implements Runnable{ run(){ ... //Tell A to repaint } }
From my understanding of swing, I need to tell A to repaint when referring directly to it
For example:
Java Syntax (Toggle Plain Text)
class X{ A a; ... a.repaint(); }
Or I could call repaint inside of Class A
Java Syntax (Toggle Plain Text)
class A{ ... repaint(); }
But how do I do this from the
run() method of class B?Thanks in advance.
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--
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--
java Syntax (Toggle Plain Text)
import javax.swing.*; public class NameSpaceClass001{ class A extends JPanel{ // blah... mumble... private B myB = null; public A(){ myB = new B(this); Thread t = new Thread(myB); t.setDaemon(true); t.start(); } } class B implements Runnable{ private A panel = null; public boolean running = true; public B(A ref){ panel = ref; } private int safeWait(int seconds){ long l = System.currentTimeMillis(); // get current time long next = l + (seconds * 1000); // get current time + future time while(l < next) l = System.currentTimeMillis(); // wait specified seconds return seconds; } @Override public void run(){ while(running){ System.out.println("Calling repaint after " + safeWait(3) + " seconds..."); panel.repaint(); } } } public static void main(String... args){ NameSpaceClass001 nsc001 = new NameSpaceClass001(); nsc001.new A(); } }
Last edited by Alex Edwards; Sep 24th, 2008 at 3:00 am.
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--
java Syntax (Toggle Plain Text)
import javax.swing.*; public class NameSpaceClass001{ class A extends JPanel{ // blah... mumble... private B myB = null; public A(){ myB = new B(this); Thread t = new Thread(myB); t.setDaemon(true); t.start(); } public B getB(){ return myB; } } class B implements Runnable{ private A panel = null; public boolean running = true; public B(A ref){ panel = ref; } public int safeWait(int seconds){ long l = System.currentTimeMillis(); // get current time long next = l + (seconds * 1000); // get current time + future time while(l < next) l = System.currentTimeMillis(); // wait specified seconds return seconds; } @Override public void run(){ while(running){ System.out.println("Calling repaint after " + safeWait(3) + " seconds..."); panel.repaint(); } } } public static void main(String... args){ NameSpaceClass001 nsc001 = new NameSpaceClass001(); NameSpaceClass001.A myA = nsc001.new A(); myA.getB().safeWait(15); } }
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
http://java.sun.com/docs/books/tutor...ncy/index.html
![]() |
Similar Threads
- Help, Help with Traffic Simulation program (Java)
- new to Swing, JTable questions (Java)
- Convert the following swing application into applet as soon as possible (Java)
- Help: need feedback on my Java assignment about thread sleep. It's already coded. (Java)
- Changing background images in frames (Java)
- Need help with threading (Java)
- how do you make java programs faster? (Java)
- Threading question... (Java)
Other Threads in the Java Forum
- Previous Thread: SMS Sender Software ( Need Help )
- Next Thread: Simple Question
Views: 505 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
6 android api apple applet application arc arguments array arrays automation binary bluetooth bold c++ chat class classes client code compare component coordinates database datagram doctype draw eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect ip j2me java javaexcel javaprojects jmf jni jpanel jtextarea julia linux list loop map method methods mobile netbeans newbie nextline number object oracle print problem program programming project recursion recursive scanner screen sell server set size sms socket sort sql string swing test threads time transfer tree user websites windows






