Threads communication

F

Federico Abascal

Please, can anybody help me?

I have a window that creates a new window in a new thread, so both
windows can receive user input. I want the parent window to know when
the child window is closed (for example). How could I do it?

(I tell you what I did: parent window creates a new thred with
itsself and in the run method I create the new window and wait for it
returns... but this way the two threads are for the parent window; I
guess it's not very nice the way I'm doing but I can't devise another
way)


Thanks!
Federico
 
Y

Yu SONG

Federico Abascal said:
I have a window that creates a new window in a new thread, so both
windows can receive user input. I want the parent window to know when
the child window is closed (for example). How could I do it?

(I tell you what I did: parent window creates a new thred with
itsself and in the run method I create the new window and wait for it
returns... but this way the two threads are for the parent window; I
guess it's not very nice the way I'm doing but I can't devise another
way)

A basic, simple way:

Write a class storing some variables (or information, objects, etc.) like
"isChildClosed", pass it to all threads and check it at a regular interval.

--
Song

/* E-mail.c */
#define User "Yu.Song"
#define At '@'
#define Warwick "warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
_______________________________________________________
 
J

Jose Rubio

Yu SONG said:
A basic, simple way:

Write a class storing some variables (or information, objects, etc.) like
"isChildClosed", pass it to all threads and check it at a regular interval.

--

You created the window in another thread, but in java there is only one
event dispatcher thread. So both window's events will be handle by the same
thread. I'm not a GUI experts, so others with more eveprtise in this area
can chime in.

What are you trying to do? If you have large calculations, then you can
exevute those on a separate thread to avoid blocking the event dispatcher
for a long period of time.
 
T

Thomas Weidenfeller

Federico said:
I have a window that creates a new window in a new thread, so both
windows can receive user input. I want the parent window to know when
the child window is closed (for example). How could I do it?

(I tell you what I did: parent window creates a new thred with
itsself and in the run method I create the new window and wait for it
returns... but this way the two threads are for the parent window; I
guess it's not very nice the way I'm doing but I can't devise another
way)

If you do what I think you do (guessed from your vague description),
then you are pretty much messing thing up.

I would like to recommend that you work through Sun's introduction
tutorial for Swing programming first, and you maybe also want to spent
some time reading Sun's TSC article regarding Swing's architecture.
Pointers to this information can be found in the comp,lang.java.gui FAQ.

/Thomas
 
S

Sebastian Scheid

Federico Abascal said:
I have a window that creates a new window in a new thread, so both
windows can receive user input. I want the parent window to know when
the child window is closed (for example). How could I do it?

Why do you create the second window in a separate thread? If you only want
both of them to receive user input then you do not need a separate thread.
Just create two JFrame objects and show() them. User input is handled
automatically in a separate thread (the EventDispatchThread) by Java.
Then you can add a WindowListener to each JFrame you are interested in and
react to the events you are interested in.

For example:

import everything;

class Main {

public static void main(String[] args) {
final JFrame f1 = new JFrame("Status 1");

JFrame f2 = new JFrame("Hello");
f2.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// Ok, the user has clicked the close button
f1.setTitle("Sorry, window #2 is not available");
}
});

f1.show();
f2.show();
}

}


Sebastian
 
F

Federico

Thanks a lot to all of you,

in fact what I wanted was not necessary to be restricted to Windows... I
found a very satisfying solution (in the spanish version of
comp.lang.java) using Observer/Observable: one window (the observer) is
advised whenever something of interest happens in the Observable object.
That's what I wanted.

Thanks,
Federico


Sebastian said:
I have a window that creates a new window in a new thread, so both
windows can receive user input. I want the parent window to know when
the child window is closed (for example). How could I do it?


Why do you create the second window in a separate thread? If you only want
both of them to receive user input then you do not need a separate thread.
Just create two JFrame objects and show() them. User input is handled
automatically in a separate thread (the EventDispatchThread) by Java.
Then you can add a WindowListener to each JFrame you are interested in and
react to the events you are interested in.

For example:

import everything;

class Main {

public static void main(String[] args) {
final JFrame f1 = new JFrame("Status 1");

JFrame f2 = new JFrame("Hello");
f2.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// Ok, the user has clicked the close button
f1.setTitle("Sorry, window #2 is not available");
}
});

f1.show();
f2.show();
}

}


Sebastian
 
R

Roedy Green

Then you can add a WindowListener to each JFrame you are interested in and
react to the events you are interested in.

if the thing you do takes a long time, you can spin it off in its own
thread in the event handler. You don't want to tie up the event
thread, because nothing can get painted and no clicks responded to.

see http://mindprod.com/jgloss/thread.html
 

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

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top