Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Applets, Dialog Frames and Threads

Posted on 1998-09-03
3
Medium Priority
?
248 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 600 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

971 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