Passing object between dialogs. please help

I have a panel which displays student's info.

There is a button called "Create New Student"

When the button is clicked, a new Dialog will shows up, where users can enter basic info for new student.

My question is, after a user finish entering data for new student, a new Student Object will be created.

How do I pass this Student Object back to original frame?

The basic question would be how do I pass object from a Dialog to its' caller Panel.

Thanks

[497 byte] By [tsongweia] at [2007-11-26 12:30:56]
# 1

You should use interfaces to communicate between components.

Something like this..

public class A implements B.StudentUpdaterInterface{

...

public void addStudent(Student student){

...

}

private void showDialog(){

new B(this);

}

}

public class B{

static interface StudentUpdaterInterface{

public void addStudent();

}

public B(StudentUpdaterInterface sui)

...

}

...

}

BlairTheMagiciana at 2007-7-7 15:42:15 > top of Java-index,Desktop,Core GUI APIs...
# 2
sorry I don't really understand the code...Where should the Student Object be?Thanks.
tsongweia at 2007-7-7 15:42:15 > top of Java-index,Desktop,Core GUI APIs...
# 3
Put your Student object in global scope or in such a scope that both the dialog boxes can access it!
DhruvaSagara at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 4
> Put your Student object in global scope or in such a> scope that both the dialog boxes can access it!That is not a good idea.
zadoka at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 5
> sorry I don't really understand the code...> > Where should the Student Object be?> > Thanks.Class B is your dialog. It calls the method on the interface that A implements to add the student.
zadoka at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 6
I am sorry but can you (Zadok) pleae tell me why taht is a bad idea?That is just what I do...so maybe I am doing something wrong...
DhruvaSagara at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 7
> I am sorry but can you (Zadok) pleae tell me why taht> is a bad idea?> That is just what I do...so maybe I am doing> something wrong... http://en.wikipedia.org/wiki/Information_hiding
zadoka at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 8

Student class can be anywhere (just about). It can be within a package containing A and B or in some package imported by A and B, or even a static inner-class of A or B.

just import it properly:

(some external package)

some.package.Student student = new some.package.Student();

and reference it properly:

(e.g. static inner class)

A.Student student = new A.Student();

or Direct reference from within package:

(if Studen is defined in its own file, within same package as A and B)

Student student = new Student();

it doesn't matter, as long as Student is accesible(properly declared public or protected) and it's package is on your classpath

BlairTheMagiciana at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 9

> Put your Student object in global scope or in such a

> scope that both the dialog boxes can access it!

>That is not a good idea.

I think there is some misunderstanding here.

Putting Student object at global scope has nothing to do with encapsulation. Furthermore, it is a requirement! In OP's main panel he/she needs reference to Student object. In Student creation dialog, we need to create a new Student. These two requirements necessitate the accesibility of Student class.

BlairTheMagiciana at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 10

> > Put your Student object in global scope or in such

> a

> > scope that both the dialog boxes can access it!

>

> >That is not a good idea.

>

> I think there is some misunderstanding here.

> Putting Student object at global scope has nothing to

> do with encapsulation. Furthermore, it is a

> requirement! In OP's main panel he/she needs

> reference to Student object. In Student

> creation dialog, we need to create a new

> Student. These two requirements necessitate the

> accesibility of Student class.

Yes, they both need to see the class. But it would be a bad idea to make the object that is in the main panel a global variable for the dialog to update.

zadoka at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 11
I got it now!!This is a new way of thinking for me...I use interface for Buttons and other widgets, never thought I can use it for Dialogs.This really makes sense!!Thank you all!!!
tsongweia at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...
# 12
thanx for clarification Zadok ;)
BlairTheMagiciana at 2007-7-7 15:42:16 > top of Java-index,Desktop,Core GUI APIs...