Communication between two Jpanels

Hi all!

I have the following problem. I am developing a db application. I have a Jframe with a table and a button. The button opens another jframe that is used to insert a user. The table of the previous panel lists all the users in the db. When I enter a user I want to update the table and repaint the first jframe, so that the table will have the new user.

I have done this by passing a reference of the the first jpanel to the second jpanel. I do not think that this is the optimal way to achieve the reaction that I want. I believe I should use listeners. Should I use ActionListener? How can I achieve what I want? An example code will be extremely useful.

Thanks for your time and effort.

Nick.

[734 byte] By [cs98nt1a] at [2007-10-1 16:09:16]
# 1

> I have done this by passing a reference of the the

> first jpanel to the second jpanel. I do not think

While not pretty, this is often the best way to do this sort of thing. Another thing you can do is set up your panels as Observer / Observable, or you can pass some sort of callback object to panel 2. However the way you've been doing it is definitely the best under the KISS paradigm ('KISS' as in Keep It Simple (Stupid) lol)

> that this is the optimal way to achieve the reaction

> that I want. I believe I should use listeners. Should

> I use ActionListener? How can I achieve what I want?

> An example code will be extremely useful.

>

> Thanks for your time and effort.

> Nick.

tjacobs01a at 2007-7-11 0:14:33 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2

Just to add a few things to tjacobs01's post:

If you always are going to use the two panels together, then go ahead and keep a reference of one panel in the other. Simple and clean. But if you one day would like to resuse just one of the panels in another application, then it's better to have some sort of listener functionality so that they can both be used individually.

The trick is to know when to keep things simple, and when to leave doors open for reusability. And that really depends on your particular case and needs.

Torgila at 2007-7-11 0:14:33 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...