Solved

Applets, Dialog Frames and Threads

Posted on 1998-09-03
3
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

696 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