This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get object from one class to another class?

 
Phil Robinson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Phil Robinson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic