Threads?

S

S

I have no clue about threads, but imagine it would be a good thing to
use in the following scenario:
I have a JApplet which makes a SWT-GUI, a class that extends JComponent
(for drawing a figure on a JCompoent inside the SWT-GUI) and a
Calculation.class that does all the calculations.

When a button is pushed I want to:
1. Calculate the right data via the Calculate.class
2. _Wait untill all the calculations are saved and updated
3. Paint the new JComponent from the JComponentMy.class with the NEW
values from Calculate.class

I have tried to find some tutorials about Threads, and how to implement
them, but have not succeeded. Is there an easy way to wait untill a
call is finished before continuing?

Thanks in advantage
 
V

Viator

I think you want to do a long operation on the click of the button and
still want the GUI to be responsive. Am I right?


Amit :)
 
S

S

I want to do a lont operation on the click of the button, then draw the
JComponent with the new values. The problem now (I think) is that the
Calculation is not finished before the JComponent starts drawing -
resulting in error drawing the values.
 
V

Viator

The components are drawn in the event dispatch thread - the same thread
that handles the button click event. So if you do your calculation in
the same thread the component will not be drawn before the calculation
is over, but the GUI will not be responsive - it might well go blank
- for that period of time. If you want the GUI to be responsive during
that period then you have to do the calculation in a separate thread
and then draw the values. For the period of time that calculation ids
being done the values in the component will be invalid. For that you
can show a message to the user like "please wait while processing...".
This is a standard practice to freeze certain parts of GUI during long
operations - like the button that submits the calculation for the
period of the operation.

Regards,
Amit
 
S

S

The following code is in the actionPerformed when pushing the button:

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")){
this.jTextAreaCommandLine.setText(this.jTextAreaCommandLine.getText()+"\nStarted");
this.run = true;
while (run) {
this.nextGen(); //Updates all the necessary datas in the
Calculations - THIS have to be finished before going to the next point
jComponentRobotArm1.paintREAL(this.getGraphics()); // This will
(hopefully) paint the JComponent with the new values - so it have to be
started AFTER this.nextGen() is finished
this.jButtonStartPause.setText("Pause");
}
}
}

So how can I make a new thread inside this method, so it finnishes
before the jComponentRobotArm1.paintREAL(this.getGraphics()) is called?

Thanks for your fast reply
 
Z

zero

The following code is in the actionPerformed when pushing the button:

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")){
this.jTextAreaCommandLine.setText(this.jTextAreaCommandLine.getText()+"
\nStarted");
this.run = true;
while (run) {
this.nextGen(); //Updates all the necessary datas
in the
Calculations - THIS have to be finished before going to the next point
jComponentRobotArm1.paintREAL(this.getGraphics());
// This will
(hopefully) paint the JComponent with the new values - so it have to
be started AFTER this.nextGen() is finished
this.jButtonStartPause.setText("Pause");
}
}
}

So how can I make a new thread inside this method, so it finnishes
before the jComponentRobotArm1.paintREAL(this.getGraphics()) is
called?

Thanks for your fast reply

As your code is now, it will do what you want: nextGen() will finish
before paintREAL() is called. If you're not convinced, test this by
printing some output to System.err:

public void nextGen()
{
System.err.println("nextGen starting"); // add this line

// your normal code goes here

System.err.println("nextGen ended"); // add this line
}

public void actionPerformed(ActionEvent e)
{
// normal code

while(run)
{
this.nextGen();

System.err.println("Start painting"); // add this line

// normal code
}
}

For applets under windows you can check this output by right-clicking
the java icon on the tray, and selecting "open console".
 
O

oulan bator

something like this should work !

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")){
this.jTextAreaCommandLine.setText(this.jTextAreaCommandLine.getText()+"\nStarted");
//here add it: it launches a new thread to perform your computation and
the refresh
new Thread(new Runnable() {public void run() {
// end
this.run = true;
while (run) {
this.nextGen(); //Updates all the
necessary datas in the
Calculations - THIS have to be finished before going to the next point

jComponentRobotArm1.paintREAL(this.getGraphics()); // This will
//here again
}}).start();
//end 'till there, will be executed

(hopefully) paint the JComponent with the new values - so it have to be
started AFTER this.nextGen() is finished

this.jButtonStartPause.setText("Pause");
}
}

}
 
T

Thomas Hawtin

S said:
I want to do a lont operation on the click of the button, then draw the
JComponent with the new values. The problem now (I think) is that the
Calculation is not finished before the JComponent starts drawing -
resulting in error drawing the values.

It sounds as if at some point need to do some copying. You probably
don't want to manipulate the same objects from two different threads.

Either paint with a copy of the results. Or clone the state within the
EDT, work on your private copy of the data in your own thread, and then
copy back in the EDT within EventQueue.invokeLater.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

threads and GUIs 18
THREADS - SOCKETS 2
perl threads 2
The distinction between a java applet and an application 1
Exceptions in Threads (& MVC) 3
Graphics help please 18
comp.lang.java.gui FAQ 0
Threads and a GUI 5

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top