• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass object with properties to another object

 
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Ranch Hand
Posts: 35
Netbeans IDE Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
type "code" inside of [] at the beginning of your code
type "/code" inside of [] at the end of your code
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes sorry it is a variable name. I changed it.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic