Solved

Applets, Dialog Frames and Threads

Posted on 1998-09-03
3
215 Views
Last Modified: 2012-05-04
  I have an applet that opens a class I derived from the frame class, and which is a form to enter user input to query a Database. When the frame closes, I need to call a couple of the Applets methods(repaint). Using Java 1.01, I've found that I need to spawn a new thread and make my frame-derived class implement the runnable interface. How should I kill the thread and how can I get the applet to know that the thread has been killed?
tgoglia
0
Comment
Question by:tgoglia
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
diakov earned 200 total points
ID: 1223537
Hi 'tgoglia',
You know that there are two ways to create a thread. By extending the Thread class and overriding its run() method, or by creating a new Thread object, that has as target a class that implements Runnable interface. In your case you have the second type, so you need to create a Thread class giving to its constructor the value of you object implementing the runnable. This could be done in the constructor of the class itself, like new Thread(this). When you call threads object method start() the thread starts it's work and the run () method in the class that implements the Runnable interfaces is called. The most safe way for a thread to finish it's work is it's run() method to finish work. Usually in the run() method you put a conditional while that does something (loading files, doing calculations, etc.) which upon an event (could be a variable set to true or false) finishes looping and the run() returns.

You can control the state of the frame through, for example, a variable set to the parent frame, so when the thread has to finish, the parent can put a false somewhere in its data members and the thread run() methods will see it and finish. Or when the run () method is about to finish, the parent object is notified through a method call, lets say, parent.notifyParent().

Another way to stop a thread is to call it's stop() method. Then it stops immideately, but any work it might have been doing is of course unfinished.

Cheers.
0
 

Author Comment

by:tgoglia
ID: 1223538
Hey diakov,
Thanks for your help. A few points I need clarified-
 
   I have the Applet named Calendar, which opens a class dialogBox which extends the frame class and implements Runnable. so the code to spawn the thread is:

dialogBox alertBox=new dialogBox(errorString,1);
Thread windowThread=new Thread(alertBox);
windowThread.start();

This calls the run() method of the dialogBox class. Do I then call the resize(),move(), and show() methods  of the dialogBox from the run method or from the parent applet?
   Right now, I have the dialogBox handling all of its own events- if the run() method is in an infinite loop, can the frame do anything else(respond to other events that occur over it, repaint itself, etc...)?
 
0
 
LVL 8

Expert Comment

by:diakov
ID: 1223539
The run() method executes in a separate thread, that is what you actually did when you created the new Thread object this way. This means that the so defined dialog box will process events as usuall. Also, the code within the run() method can call dialog methods thus adjusting interface elements (let say a progress bar or something). The applet it self can also do this, depending on whether the dialog is modal.

If you want to resize and position the dialog window, do this from the parent (the applet) where the dialog is created. Then call show and it will show. The run method of the dialog starts anyway but usually should be used for something else, for example background calculations or animation sequences in the dialog box.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now