943,892 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 773
  • Java RSS
Dec 10th, 2008
0

using methods in different classes.

Expand Post »
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:
java Syntax (Toggle Plain Text)
  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:
java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 4
Junior Poster
PhiberOptik is offline Offline
164 posts
since May 2008
Dec 10th, 2008
0

Re: using methods in different classes.

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.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 10th, 2008
0

Re: using methods in different classes.

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.
java Syntax (Toggle Plain Text)
  1. balance = JFrame.getInstance().getBalance();
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Dec 10th, 2008
0

Re: using methods in different classes.

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:
java Syntax (Toggle Plain Text)
  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. }
java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 4
Junior Poster
PhiberOptik is offline Offline
164 posts
since May 2008
Dec 10th, 2008
0

Re: using methods in different classes.

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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java coding problem
Next Thread in Java Forum Timeline: Receiving Sms from mobile through Gateway using java..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC