aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Pass object with properties to another object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Pass object with properties to another object" Watch "Pass object with properties to another object" New topic
Author

Pass object with properties to another object

Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Good Day All!

I am trying to find either some references to point me on the right track or some help with passing an object with all of it's properties still in tact after it's been created. Currently I am trying to do this through an interface but it seems to just create a new object everytime without the properties. Example below (also please forgive me but for some reason this site does not like the browser I am using so there is no option for code tags...):



When I implement the interface on the other objects as soon as I call the setP method shown above it seems to just create a new one even though I pass the object to the method I want to use. Can someone expound on this a little please? Thank you for your help and time,


TheChazm
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Calling that setP method will not create a new Person object.
If a new Person object is being created then it is being done elsewhere - why don't you show us the code that calls that method.
Also you say it seems to create a new object - what makes you think a new object is being created ?


Joanne
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2233
    
  47
To be sure what is happening we need to see the calling code as well.
Once setP() is called no new object is created, the reference to the object passed in is stored in the instance variable p. It may be that in the calling code you are reusing the People object you have passed in to setP().
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Ok no problem. Below is the calling code that requests the object from the interface.

When I call the below code:

Person p = ThrowerInstance.getP();
this.add(p, this.Getgbc(p));

It error's out on this.add(p, this.Getgbc(p)); because inside of the Getgbc code it calls for the property from the person object and it says it's null which it was not.

Here is the code for the Getgbc:


Thank you for the replies and again sorry for no code tags...

Jacob Draper
Ranch Hand

Joined: May 20, 2014
Posts: 33

type "code" inside of [] at the beginning of your code
type "/code" inside of [] at the end of your code
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2233
    
  47
Chaz Branham wrote:Ok no problem. Below is the calling code that requests the object from the interface.

We asked for the code that calls the setP() method not the code that calls getP().

Are you sure you are using the same instance of your Thrower class when setting and getting P?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Chaz Branham wrote:Ok no problem. Below is the calling code that requests the object from the interface.

When I call the below code:

Person p = ThrowerInstance.getP();
this.add(p, this.Getgbc(p));

It error's out on this.add(p, this.Getgbc(p)); because inside of the Getgbc code it calls for the property from the person object and it says it's null which it was not.

You still haven't shown us the Person object being created.
You've also introduced a new class (ThrowerInstance). How is this related to the code you originally posted ?
And which property of Person is null ? The only method you call on the Person instance appears to be one that returns a boolean and that's a primitive value so can't be null.

And by error's out do you mean it throws an exception ? If so, show us the stack trace of the exception.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2233
    
  47
Joanne Neal wrote:And which property of Person is null ? The only method you call on the Person instance appears to be one that returns a boolean and that's a primitive value so can't be null.

I suspect it's 'p' that is null, hence my comment using the same instance of the Thrower class.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Tony Docherty wrote:
Joanne Neal wrote:And which property of Person is null ? The only method you call on the Person instance appears to be one that returns a boolean and that's a primitive value so can't be null.

I suspect it's 'p' that is null, hence my comment using the same instance of the Thrower class.

That makes sense. So ThrowerInstance is probably a badly named variable rather than another class.
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Tony Docherty your question tipped me off:

Are you sure you are using the same instance of your Thrower class when setting and getting P?

That's exactly where I went wrong.. How would I use the same instance of the thrower class between multiple classes?

Thank you so much!

Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Also to answer the question on the setP method below is the code. Going to try and put the code tags around it.

Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Do I just setup the thrower class on the parent of the JPanels and have them call something to the parent to keep it just one thrower class?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Is ThrowerInstance a variable name or a class name ? If it's a variable name can you please change it to throwerInstance. Java convention says variable names should start with lower case.
The way you've got it makes it look like a call to a static method of the ThrowerInstance class and it's confusing my poor old brain
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Yes sorry it is a variable name. I changed it.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2233
    
  47
Chaz Branham wrote:How would I use the same instance of the thrower class between multiple classes?

There are number of ways such as passing the instance reference to each object that needs to use it or if all the objects are owned by the same parent or they all have a have a refence to a common object put an accessor method in the object etc. The most important thing before doing anything though is to decide which class should sensibly own the Thrower object and should you be passing references to it around or should you be passing a reference to the person class or should you provide some other strategy altogether.

Without knowing what you are trying to do and how you have designed your program it's difficult to give more precise information.
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Tony thank you for the detailed response. The purpose of this tool is to drag one person object from one JPanel to another with all of it's properties because the data is being stored in a database using sql statements to adjust, add, and read what should be displayed or updated. The JPanels parent is a JLayeredPane that is added into a JFrame with a JScrollbar for added space. Major reason for the JLayeredPane is to produce the DND effects by using a BufferedImage and uses some alpha blending and changing the z access layer to make sure it stays on top of all the others.

I hope this makes more sense but I need the same person object to move to the other JPanel when dragged to it is the entire goal.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Chaz Branham wrote:Also to answer the question on the setP method below is the code. Going to try and put the code tags around it.



I must preface this comment by saying I know nothing about drap and drop in Swing and it's a long time since I've done any Swing programming, so there may be some idiosyncracy of Swing that makes what I'm about to suggest impossible or hard to implement. If that's the case feel free to ignore it.

Having said that, the fact that this method comes from your Person class makes your design a bit suspect. The Person class (classes in general in fact) should only contain fields and methods related to what a Person is and what it can do.
It should contain nothing related to what is going to be done to it. If you want to drag and drop an instance of it, that's fine, but none of the code to implement that dragging and dropping should be part of the Person class.
If you separate out your drag/drop code into a separate class, you may find it easier to move your Person instance around.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2233
    
  47
It's a long time since I've done any drag and drop but I thought the object(s) being dragged was accessible through the DragGestureEvent.
May I suggest you read the tutorial on DnD.

As this is a DnD issue I'll move this thread to the Swing forum as you may get better answers there.
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Thank you Joanne and Tony. I believe the code needs to be there but I'll do some research and Tony I have read the Dnd tutorials and what I am trying to do really has nothing to do with DND because I am not using DND to transfer any data. When using Dataflavors it seems to be to restrictive when dealing with objects and data so I am only using the DND for triggers and visual effects. The gist of the whole problem I guess is I don't know how to properly move an object from class to class without it creating a new one.

I'm going to try and see about adding the interface to the JLayeredPane but I'm not sold on if this would be the best answer as to handle this situation.

Any other thoughts?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2233
    
  47
I have read the Dnd tutorials and what I am trying to do really has nothing to do with DND because I am not using DND to transfer any data. When using Dataflavors it seems to be to restrictive when dealing with objects

In what why is it restrictive?
I've dug back through some old code and I transferred an array of objects by setting a data flavour is follows:

The gist of the whole problem I guess is I don't know how to properly move an object from class to class without it creating a new one.

I'm still not sure what your real problem is. Can you describe in words, without reference to any code, exactly what you are trying to do.
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Thank you for the reply Tony. When I tried using dataflavors to transfer the object it would always create a new instance of the object instead of using the one already created so after testing it many times and trying many different things it just never worked. I've spent almost three months off and on trying to just figure out how to transfer the same object to another object. Today actually I figured out something that seems to work very well. I don't know if there are any pitfalls in doing it this way but have a look at my code below.

I created a shared instance class:


Then I simply call the following at the start of the drag:


And finally the recieving JPanel has this code:


It seems to work great for passing objects around to other objects and is simplistic. Like I said earlier though I don't know if there are any pitfalls in doing it this way but it sure does work
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2233
    
  47
It seems to work great for passing objects around to other objects and is simplistic. Like I said earlier though I don't know if there are any pitfalls in doing it this way but it sure does work

It's so full of potential pitfalls I don't know where to start but whether they effect your code or not is hard to say because you still haven't told us what you are trying to achieve and without that information I can not help you further.
You are fixated on using a solution and all your questions are related to solving issues with using that solution but it may not be the best solution to your problem. Please tell us WhatNotHow.
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Tony I apologies if I had not been clear with my goal. I am (trying) to write a manning tool to visually assign personnel to work. I need it to be drag and drop oriented instead of a data entry screen. The overall goal is when you drag a person and drop them somewhere it will put the person there and make the change in the database so reports and other items can be ran off of that data.

So the tool is just an interface for meeting needs by assigning personnel and keeping track of those changes and assignments.

Does that make sense? Is that what you were asking about?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2233
    
  47
Ok, The data flavour code I posted earlier was pulled from a workflow application I wrote some time ago which presents lists of tasks to be completed and people who can do them and the GUI works by the user dragging the tasks onto the person that they want to assign the tasks to. The act of dragging and dropping reassigns the task, updates the DB, updates the persons personal task list on their computer etc and works perfectly well using the standard DnD mechanism. This sounds incredibly similar to what you are doing so I fail to see why you need to invent your own way of doing it unless you have some design issues elsewhere in your application you are trying to overcome. I think you should stop coding and think hard about how to use the DnD mechanism to solve your problem. If it can't do what you need maybe it's time to have another look at your design.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't see the point of that PassObjects class. As far as I can see it's a singleton whose only purpose is to contain a reference to an object. Why not just use the reference to the object itself instead of wrapping it in that singleton and then unwrapping it again?
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Tony. Thank you for your response as that is what I thought I could have done with the DND library but since I am a beginner (even though I have went through every tutorial I could find) it has eluded me. For example:


This code is located in the drop method of the recieving JPanel.


And the code for the Person being dragged I used the following to create the DataFlavor:



It would execute without errors which I was happy about but the person object created from the dataflavor was always a new person as I believe from the following line did that:



So after a very long time I decided that it was too restrictive to do what I needed which was probably the wrong decision but I had nothing else to go on. I wished I could have found a good example or something simalier that would just transfer a component using dnd instead of just creating a new one each time. There may be something like that out there but I could not find it.

Paul I would love to if I understood what your talking about. I'm not sure how to accomplish that.

Thanks again everyone and I know a lot of folks are probably shaking their heads at me but I really want to understand it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What I have done in my DND code is to pass the object being dragged in my TransferHandler, rather than having the source stash it somewhere and then having the target retrieve it.
Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Paul, not to sound like an idiot but is that not what I did with the following code or did I miss something?:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, that looks very much like the code I have:



As far as I know the DND support isn't cloning my BNBTreeNode objects either, so I don't know what's going on with your Person objects.

Chaz Branham
Ranch Hand

Joined: Apr 04, 2013
Posts: 50

Ahhh I think it has something to do with your following line:



I didn't know of TransferSupport. I bet that has some key to play with getting the object but I'll have to do some research. Thanks for the example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass object with properties to another object