aspose file tools*
The moose likes Beginning Java and the fly likes Why does this refuse to work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does this refuse to work?" Watch "Why does this refuse to work?" New topic
Author

Why does this refuse to work?

Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
Hi everybody,
I am pretty new to Java but not new to programming. I am having trouble with one of my projects.
Background:
I am writing an applet that extends the class JApplet and implements ActionListener. The Applet simulates a cellphone. I have created the interface with basic swing components like buttons and textboxes. They phone does not actually dial numbers only looks like it does. It has all the number buttons and a few other functions like redial and hangup etc. I have all of this working. I also have a button called Talk which dials the number the user has typed and displays the results in the text box. I have added event listeners to all buttons that the user can click and my problem lies with the code in the Talk button event listener.
Problem:
As mentioned above my problem lies with the Talk button and the event listener for it. Once the user has finised clicking the buttons they want and hit the talk button my code first displays the number the user has typed in the textbox then goes through and randomly displays a response to the call like a busy signal, sorry message or ringing. I have all of this working but the initial part of displying "Dialing <use number> ... I am not sure why it is not working, I don't get any compilation or runtime errors. It's as if this statement is being skipped when the applet executes. I have the response(ex: sorry, busy etc) writing to the same textbox as the displaying the number, so I realized that the first message will be overwritten by the second. To correct this problem I implemented a variety of delay mechanism like loops that execute 100000 times or using a timer or Thread.sleep(1000) etc. No matter what I used I can't get the first message to appear in the text box. Here is my code. Any help would be greatly appreciated. Thanks.

Code:

Sorry if the formatting is a little crappy, best I can do on this web page. It does compile and run but I am having trouble with the statements
textfield1.setText("Dialing " + numbertyped + ".....");
myTimer.start();
I have initialized the timer in another part of my code, which is not included here.
[ February 08, 2003: Message edited by: Thomas Knight ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Thomas,
Without actually setting up a test, I can't be totally sure what your problem is, but my instincts tell me that it lies with your delay mechanism. If you want painting events to occur normally in swing then you need to make sure that everything occurs on the swing event thread. I would suggest that you try using a javax.swing.Timer which was designed to work with swing.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
Hi Michael,
Thanks for the reply. I don't know what you mean when you say, "you need to make sure that everything occurs on the swing event thread." Also I am importing the following..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
I think I am using the Timer that you are talking about. I declared it as

I am not sure what is going on. No matter if I put a 5000 milisecond delay or 1000 mili second delay I can't get the statement


to work. Any other ideas??

Originally posted by Michael Morris:
Hi Thomas,
Without actually setting up a test, I can't be totally sure what your problem is, but my instincts tell me that it lies with your delay mechanism. If you want painting events to occur normally in swing then you need to make sure that everything occurs on the swing event thread. I would suggest that you try using a javax.swing.Timer which was designed to work with swing.
Hope this helps,
Michael Morris
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
Try Thread.sleep(5000) immediately after you set the text to "Dialing...". That should work for you.
Here is a small example, look at the go() method.

[ February 08, 2003: Message edited by: Chad McGowan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does this refuse to work?