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

How to get object from one class to another class?

Phil Robinson
Greenhorn

Joined: Nov 09, 2010
Posts: 5
So, I am working on a java project which includes a JFrame with 4 JPanels (North, Center, East, South). I also created a class (Total) with a getter, a setter, and a toString method. So if i created an object in the east panel ( muffin) and then i set the value of muffin to 12 (so the code i put in is "muffin.setValue(12);"), how would i access the object muffin in the north panel so that i can do whatever i need to do with it? Can anyone help me? Here is my JFrame, the NorthPanel, and the CenterPanal (only a bit of the CenterPanal, because there is a lot of unnecessary code that i don't want to bog you down with). I would really appreciate it if anyone could help me!










pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Hello Phil and welcome to the Ranch!

I took the liberty of adding code tags so your code will retain its formatting. To learn how to do this yourself from the FAQ's here: FAQ's


As for your question, one possible solution is to give your classes getters and setters for the food items or for properties of the food items that need to be shared.

Much luck and again, welcome!
Phil Robinson
Greenhorn

Joined: Nov 09, 2010
Posts: 5
Hey Pete, thanks for the advice, but do you know how i would actually set a getter method for an actual object?? I have literally spent more than 5 hours trying to figure this out, and i cannot find anything on how to get this to work. Thanks!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Phil Robinson wrote:Hey Pete, thanks for the advice, but do you know how i would actually set a getter method for an actual object??


I'm not sure what you're stuck on. A getter for an object is usually no different from any other getter mtehod.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10355
    
    8

Phil Robinson wrote:... how i would actually set a getter method for an actual object??

Recommended reading http://download.oracle.com/javase/tutorial/java/javaOO/variables.html
Notice the getXXX and setXXX methods which can be used to access or mutate the variable values.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Phil Robinson
Greenhorn

Joined: Nov 09, 2010
Posts: 5
Hey pete. When looking at the code, in the Center Panel, i create the Total object "muffin", which i have set to a value of 12 (muffin.setValue (12);) Then i tried to get that value from the muffin object in the center to the north, but whatever i try, the north panel just tells me that it couldnt find the object "muffin". I know this is something east, but i am just really frustrated with this right now. Thanks!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Phil Robinson wrote:Hey pete. When looking at the code, in the Center Panel, i create the Total object "muffin", which i have set to a value of 12 (muffin.setValue (12);) Then i tried to get that value from the muffin object in the center to the north, but whatever i try, the north panel just tells me that it couldnt find the object "muffin". I know this is something east, but i am just really frustrated with this right now. Thanks!


Without seeing code showing the error, I can only guess, but did you give the CenterPanel a getMuffin method? Did you call this method on a CenterPanel variable (not a JPanel variable)? Does this variable have a reference to the CenterPanel that is being displayed? Can you post a small compilable or nearly-compilable (except for the error you get on trying to get muffin info) to the forum?

Luck!
Phil Robinson
Greenhorn

Joined: Nov 09, 2010
Posts: 5
Hey pete! Thanks for all the help, but i got it all sorted out today! I was not even CLOSE to how i was supposed to being doing it. Instead of writing a method for an object to go to another panel, i had to set a get method for the actual JPanel, which i didnt even know i could do! Thanks for all the help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get object from one class to another class?