![]() |
|
|
|
#1 |
|
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 S |
|
|
|
|
#2 |
|
Posts: n/a
|
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 Viator |
|
|
|
#3 |
|
Posts: n/a
|
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. S |
|
|
|
#4 |
|
Posts: n/a
|
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 Viator |
|
|
|
#5 |
|
Posts: n/a
|
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.jTextAreaCo mmandLine.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 S |
|
|
|
#6 |
|
Posts: n/a
|
"S" <> wrote in
news: oups.com: > 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.jTextAreaCo mmandLine.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". -- Beware the False Authority Syndrome zero |
|
|
|
#7 |
|
Posts: n/a
|
something like this should work !
public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Start")){ this.jTextAreaCommandLine.setText(this.jTextAreaCo mmandLine.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"); } } } oulan bator |
|
|
|
#8 |
|
Posts: n/a
|
S wrote:
> 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 -- Unemployed English Java programmer http://jroller.com/page/tackline/ Thomas Hawtin |
|
|
|
#9 |
|
Posts: n/a
|
I would first do some simple things that can use threads:
http://www.google.com/search?q=java+threading+example http://www.javaworld.com/jw-09-1998/jw-09-threads.html the Javaworld threading example is kinda old, but the syntax and ideas haven't changed (don't worry about platform independence... that hasn't been too much of an issue with recent operating systems..) Mike |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Cancelling Timer and TimerTask threads | wfalby | Software | 1 | 04-30-2009 01:04 PM |
| Why multiple threads are not started ? pls help | mandrake | Software | 0 | 04-21-2009 10:19 AM |
| NETFLIX has gone down the tubes | funnsun34@hotmail.com | DVD Video | 95 | 03-03-2006 03:15 PM |
| Wtd: The extended version of Stargate SG-1 episode Threads | midnightpatches@msn.com | DVD Video | 0 | 01-21-2006 02:47 AM |
| Stargate SG-1 - Season 8 - How long is the uncut version of the episode 'Threads' supposed to be? | Zygon Curry | DVD Video | 10 | 11-27-2005 07:11 PM |