File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Communication between a EJB and its container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Communication between a EJB and its container" Watch "Communication between a EJB and its container" New topic
Author

Communication between a EJB and its container

Dave Elwood
Ranch Hand

Joined: Dec 27, 2002
Posts: 84
I've got a desktop app BigForm which has a member JPanel object with special talents
The talented JPanel is an EJB project named MyPanel.

(I'm working with NetBeans).

Now I'd like the MyPanel to send events to the container BigForm, however
MyPanel doesn't see the package of BigForm.

BigForm sees the package of MyPanel, but not the other way around.

Is the following a good way to avoit mucking about with events ??? :
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Dave,

I don't know what this has to do with EJBs, or what you mean by that final question -- but I can tell you that the "new Widget(this)" pattern you're using is common and a good way to do things. Telling an object you create "here is how to get back to me" is a good idea.


[Jess in Action][AskingGoodQuestions]
Dave Elwood
Ranch Hand

Joined: Dec 27, 2002
Posts: 84
You're right, the code is not !! that of the desktop app with the EJB MyPanel. No, the code shows what I wondered could be a way out.

Actually I expected you people to say that passing a "this" to a member constructor would be really bad, recursive chaos and things like that, but your answer rested my fears.

Many Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
I think you can pass a this reference into something inside the class in your constructor. That panel is a field of the class, so it is "inside". Now, letting a this reference get outside the class from inside the constructor, that is something altogether different.

Don't Swing components have a getParent() method, and automatically record their parent component? Or have I misunderstood the problem?
Dave Elwood
Ranch Hand

Joined: Dec 27, 2002
Posts: 84
a getParent() function? Excuse me, I fell off my chair.

Very interesting. I have some testing to do now.

P.S. the "this" won't only be outside the class,
it will be outside the BigForm project and transferred to another project, the EJB MyPanel project. !!!
Dave Elwood
Ranch Hand

Joined: Dec 27, 2002
Posts: 84
well guys, getParent() doesn't work

I've got two projects...BigForm and the EJB MyPanel,
these are two different classes,
in two different packages,
in two different projects.

in MyPanel I tried to use the getParent() to call the signal() function way back in BigForm.

The call looked like this :


in the MyPanel class, the pkBigForm package is not visible !!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You might need to import that class into the panel class. Have you checked the return type of getParent()? It might not be the right return type.

If it doesn't work, it doesn't work. If the method works, so much the better, if it doesn't, you have learned something useful for future reference.
Dave Elwood
Ranch Hand

Joined: Dec 27, 2002
Posts: 84

>>You might need to import that class into the panel class.

Tried that. Didn't work. The panel can't see the BigForm class.

>>Have you checked the return type of getParent()?

Yes, it's Object. And casting it to BigForm doesn't work because
BigForm is not visible to the panel class, as I said.

>>If it doesn't work, it doesn't work.

Thanks a lot.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
No, the return type of Component#getParent is Container. You can add only a few Listeners to a Container.
Dave Elwood wrote: . . . >>If it doesn't work, it doesn't work.

Thanks a lot.
I didn't mean it like that!

Maybe you should have some sort of class which incorporates the connection, and add both the EJB and the container to it.
Or create a Mouse Listener which takes a click ffrom the container and calls a method in the EJB

Can't think of anything else to suggest. Sorry. Anybody else?
Dave Elwood
Ranch Hand

Joined: Dec 27, 2002
Posts: 84
AAAARRRRGGGHHHH

I've got it.

Events : the wrong way :

1. in BigForm call

2. the Panel has a javax.swing.event.EventListenerList to hold it in. Not in BigForm, but in Panel. Very strange.

3. the Panel creates a BurpEvent object and calls processBurps() with it. Again in the dumbest place possible,
in the event source object, Panel.

4. processBurps() picks out the MyBurpListener object and calls the BurpOccured() member.

Problem : I want things to happen in BigForm, not in MyBurpListener. I'm forced to put MyBurpListener in the Panel project
area so as a result BigForm in inaccessible to MyBurpListener. Why there? Because it's the only place to
put MyBurpListener so that both the Panel project and the BigForm project have access to it.

Solution : Anonymous Inner Class

Events : the right way :

1. in BigForm call


2. - 4. as above

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Communication between a EJB and its container