Solved

Applets, Dialog Frames and Threads

Posted on 1998-09-03
3
217 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
micro services vs rest web services 16 86
Securing Jmx Console and web console 2 65
tomcat not starting 6 31
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

896 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

12 Experts available now in Live!

Get 1:1 Help Now