Event posting and handling in all classes?

K

Kaidi

Hi guys,
I have one question of how to post/hand events under this class
framework:


JFrame A
JPanel B
JPanel C JPanel D .....
JPanel E, Thread F. JPanel G, Thread H. ... ...

Thread F will be called when a button in JPanel E is clicked.
And thread H will be called when a button in JPanel G is clicked.
C,E,F are one one group. D,G,H are another group, etc. As program goes
on, more such groups will be added.
Thread F will update some GUI display things in JPanel E and D and G.
Thread H will also need to update some display things in JPanel G, D.
C.
In other words, each thread needs to update something in ALL other
panels.

Surely, one way is to keep an reference of each of these panels in B
(or frame A) and keep a "parent" reference in each panels to B (or A).
But besides that, is there any better way to use Event to do this? For
example, thread F just posts an event and all other JPanels can get
and process.

Any suggestion or code example are appreciated. :)

Thanks.
PS: I am kind of new in Java event. I use JBuilder for
button/mouse/JComponents events and the handling functions are
auto-generated by JBuilder.
 
T

Thomas Weidenfeller

Kaidi said:
In other words, each thread needs to update something in ALL other
panels.

Two things:

1. It is a bad idea that code updates panels. Code should update models.
You might want to read about the so called Model-View-Controller
architecture / design pattern (MVC), and the variant of it implemented
in Swing.

2. Swing and AWT are not thread save. You will have to schedule any such
panel update on the EDT if you don't use some kind of MVC. Even if you
use some kind of MVC, you have to make sure that you protect your models
properly and that the notifications you fire end up at the EDT.

Some more hints and pointers to resources can be found in the
comp.lang.java.gui FAQ.

/Thomas
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top