using methods in different classes.

Reply

Join Date: May 2008
Posts: 128
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

using methods in different classes.

 
0
  #1
Dec 10th, 2008
I created a GUI. with a bunch of classes (that extended JPanel) and one that extends JFrame and now I can't figure out how to transfer data between them. lets say I have a public variable set in the JFrame class and a get method like the one below:
  1. public double getBalance()
  2. {
  3. return balance;
  4. }
How do i access this get method from one of the panel classes so I can update the GUI? I tried adding:
  1. double b=atmEngine.getBalance();
but it gave me this error:
non-static method getBalance() cannot be referenced from a static context
I thought about designing a send method from my launcher but I just don't understand how this works!

(Btw this is my first large GUI program sorry for all the questions I am trying as hard as I can to figure it out myself )

Thank so much,
PO.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: using methods in different classes.

 
0
  #2
Dec 10th, 2008
The panel class needs a reference to your frame class. Assuming your frame class creates the panels, the frame should send a reference to itself (this) as an argument to the panel constructor. You will need to change the panel constructor to accept this argument and store it. Now when the panel wants to call a method in the frame class you can use that reference.

EDIT: one more thing, you should generally not make the variable in panel public, that's what the public get method is for. Make the variable private and the get method public.
Last edited by mahlerfive; Dec 10th, 2008 at 3:33 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: using methods in different classes.

 
0
  #3
Dec 10th, 2008
One approach would be to put a static method inside the JFrame class called getInstace() that would return an instance of the JFrame class (not necessarily new instance) you can restirct the instance to be single. Once this instance is returned you can call a non-static method on it.
E.g.
  1. balance = JFrame.getInstance().getBalance();
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: using methods in different classes.

 
0
  #4
Dec 10th, 2008
Okay so I'm trying to figure out how to use the 'this' keyword. I have no idea where to use this. From what I can understand from my textbook it has to be in the atmEngine class and then the FunctionPanel class will get it in using a constructor problem is I don't know how to send it to the constructor.
Here are both of my classes:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.text.DecimalFormat;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. public class atmEngine extends JFrame
  9. {
  10.  
  11. private double balance; //Here is where balance is set in the class
  12. private TitlePanel title;
  13. private FunctionPanel function;
  14. private AccountBalancePanel info;
  15. private NumberPanel numPad;
  16. private FastCashExitPanel fastCashExit;
  17.  
  18. public atmEngine() throws IOException
  19. {
  20. //Window Title
  21. setTitle("Jon's ATM Interface");
  22.  
  23. //Using close button
  24. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.  
  26. //creating layout manager
  27. setLayout(new BorderLayout());
  28.  
  29. //setting size
  30. //setSize(700, 10800);
  31.  
  32. //Creating Panels
  33. title = new TitlePanel();
  34. function = new FunctionPanel();
  35. info = new AccountBalancePanel();
  36. numPad = new NumberPanel();
  37. fastCashExit = new FastCashExitPanel();
  38.  
  39. add(title, BorderLayout.NORTH);
  40. add(function, BorderLayout.WEST);
  41. add(info, BorderLayout.SOUTH);
  42. add(numPad, BorderLayout.CENTER);
  43. add(fastCashExit, BorderLayout.EAST);
  44.  
  45. pack();
  46. setVisible(true);
  47.  
  48. }
  49. public void setBalance(int accountNumber) throws IOException
  50. {
  51. String accountFile="accounts/accounts.csv";
  52.  
  53. int accountTranslation=0;
  54. boolean accountVerified=false;
  55.  
  56. FileReader freader = new FileReader(accountFile);
  57. BufferedReader outputFile = new BufferedReader(freader);
  58.  
  59. String str="404";
  60.  
  61. while((str!=null)&&(accountVerified!=true))
  62. {
  63. str = outputFile.readLine();
  64. if ((str!=null) || (accountTranslation==accountNumber))
  65. {
  66. String[] tokens = str.split(",");
  67. accountTranslation = Integer.parseInt(tokens[0]);
  68.  
  69. if(accountTranslation==accountNumber)
  70. {
  71. balance=Double.parseDouble(tokens[1]);//here is where to find balance
  72. accountVerified=true;
  73. }
  74. else
  75. {
  76. accountVerified=false;
  77. }
  78. }
  79. }
  80. if(accountVerified==false)
  81. JOptionPane.showMessageDialog(null, "Account does not exist");
  82.  
  83. }
  84. //this method returns balance
  85. public double getBalance()
  86. {
  87. return balance;
  88. }
  89. }
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class AccountBalancePanel extends JPanel
  5. {
  6. //private double b=atmEngine.getBalance();
  7. public boolean withdrawDeposit = true;
  8.  
  9. private JLabel balance;
  10.  
  11. public AccountBalancePanel()
  12. {
  13.  
  14. balance = new JLabel("Account Balance: $0.00");
  15.  
  16. setBorder(BorderFactory.createTitledBorder("Current Information"));
  17.  
  18. add(balance);
  19. }
  20. //below is the constructor that accepts the balance
  21. public AccountBalancePanel(double accBalance)
  22. {
  23.  
  24. balance = new JLabel("Account Balance: $"+accBalance);
  25. System.out.println(accBalance);
  26. setBorder(BorderFactory.createTitledBorder("Current Information"));
  27.  
  28. add(balance);
  29. }
  30. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: using methods in different classes.

 
0
  #5
Dec 10th, 2008
I just spent 5 minutes looking over your code and I have no idea what you're talking about. Could you explain what you want each one of your classes to do? Then, explain which of these classes is not working like you want it to, and explain how you want it to work. You only posted two classes, neither of which contains a main method. And you're talking about FunctionPanel, but you didn't say what FunctionPanel does. Also, post the code for any classes that you want help with that currently aren't working.

Btw, to further explain what others have said, here is what you want to do:
  1. If you want class ATM to be able to update class BankGUI, then the constructor for ATM would look like this:
  2.  
  3. public ATM(BankGUI theGUI, ....){
  4. atmsBankGUI = theGUI; //ATM has to have a variable called atmsBankGUI
  5. }
  6.  
  7. And lets say ATM lets you withdraw money.
  8.  
  9. public void withdraw(){
  10. int amount = dollarAmount; //get the dollar amount the user wants to withdraw
  11. atmsBankGUI.withDrawCash(amount);
  12. }
  13.  
  14.  
  15. //Now, this method would be in the BankGUI class
  16.  
  17. withDrawCash(){
  18. //Do whatever you have to, to make the GUI display the withdrawal
  19. //For example, you could use JLabel.setValue to set it to the new amount of money the user has.
  20. }

If you wanted to do the opposite, the BankGUI class to be able to get input from the user, but you wanted the ATM class to do the calculations (for example, the user types the amount they want to withdraw in the BankGUI, but you want to calculate this amount in ATM class) you would do the opposite: When you invoked the BankGUI classes constructor, one of the arguments passed would be an ATM. You could also do a mixture of the two.
Last edited by BestJewSinceJC; Dec 10th, 2008 at 5:46 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC