Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing Display Problem

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I am learning about threads and I ran into a problem. My source code is below. I created a simple app to display a series of numbers, one after the other. It works but it doesn't clear the window before the next number is painted. I am using the repaint() method. If I convert the app to awt, that is extend Frame instead of JFrame, and a few other changes to get rid of swing, it works properly. I have no clue as to why this is? Is there a different method to call to achieve these results?<br><br>fenris<br>=================================================<br><br>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br>import javax.swing.event.*;<br><br>public class ThreadTest extends JFrame implements Runnable<br>{<br> Font theFont = new Font(&quot;TimesRoman&quot;,Font.BOLD,24);<br>&nbsp;&nbsp;&nbsp;&nbsp;Thread number1;//The first number to display<br>&nbsp;&nbsp;&nbsp;&nbsp;//Thread number2;//the second number to display<br><br> int count;<br> String displayStr;<br><br><br> public static void main(String[] args) <br> {<br> new ThreadTest();<br> <br> }<br><br> public ThreadTest()<br> {<br> super(&quot;Thread Testing&quot;);<br> setSize(300,300);<br> <br> addWindowListener(new MyWindowListener());<br> <br> count = 0;<br><br> number1 = new Thread(this);<br> <br> number1.start();<br> <br> setVisible(true);<br> <br> }<br><br><br> public void run()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (count &lt; 1000)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++count;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayStr = String.valueOf(count);<br> validate();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;repaint();<br> <br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number1.sleep(100);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch (InterruptedException e)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;public void paint(Graphics g)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;{ <br> g.setFont(theFont); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.drawString(displayStr, 150, 150);<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br><br><br><br> public class MyWindowListener extends WindowAdapter<br> {<br> public void windowClosing(WindowEvent e)<br> {System.exit(0);}<br><br> }<br><br>}
 
Hi!<br><br>The swing is different from awt, the swing is not multi thread safe, thus methods working on a swing component must be called from own swing thread. Your problem is that called the repaint from not the JFrame thread. The solution is invokeLater and invokeLaterAndWait methods (see the doc).<br><br>Bye, Otto.<br>
 
Otto,<br><br>As I said before I am very new to threads, and I am not sure I understand what you mean. Would you be able to modify my code to show me what you mean. I would appreciate it greatly.<br><br>Thanks in advance,<br><br>fenris <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
Hi!<br><br>Sorry, but I was wrong. Your problem is more simple, you do not need invokeXXXX methods, only override the JFrame update method. In the awt the Frame.update() clears the area and call paint(), but in the swing the JFrame.update() just call the paint(). Put this code into your source:<br><br>public void update(Graphics g) {<br>&nbsp;&nbsp;getContentPane().update(g);&nbsp;&nbsp;// calls the compent update, (clear)<br>&nbsp;&nbsp;paint(g);&nbsp;&nbsp;// calls the JFrame paint<br>}<br><br>Bye, Otto.<br>
 
Thank you very much Otto for the help. I had done a bit of reading, but didn't quite understanded it. I had seen the update method for AWT but could not find one for Swing. The over ridden update method is what I thought the problem was, but I didn't know how to rewrite it, Thanks.<br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top