Solved

Applets, Dialog Frames and Threads

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
eclipse console opening separately 2 48
Java Restore security prompts not working 10 250
JVM error from eclipse 1 26
Selenium WebElement Login button findElement 9 17
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

732 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