| | |
using methods in different classes.
![]() |
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:
How do i access this get method from one of the panel classes so I can update the GUI? I tried adding:
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.
java Syntax (Toggle Plain Text)
public double getBalance() { return balance; }
java Syntax (Toggle Plain Text)
double b=atmEngine.getBalance();
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.
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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.
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.
One approach would be to put a static method inside the JFrame class called
E.g.
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)
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 !!!
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:
Here are both of my classes:
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import java.io.*; import java.util.*; public class atmEngine extends JFrame { private double balance; //Here is where balance is set in the class private TitlePanel title; private FunctionPanel function; private AccountBalancePanel info; private NumberPanel numPad; private FastCashExitPanel fastCashExit; public atmEngine() throws IOException { //Window Title setTitle("Jon's ATM Interface"); //Using close button setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //creating layout manager setLayout(new BorderLayout()); //setting size //setSize(700, 10800); //Creating Panels title = new TitlePanel(); function = new FunctionPanel(); info = new AccountBalancePanel(); numPad = new NumberPanel(); fastCashExit = new FastCashExitPanel(); add(title, BorderLayout.NORTH); add(function, BorderLayout.WEST); add(info, BorderLayout.SOUTH); add(numPad, BorderLayout.CENTER); add(fastCashExit, BorderLayout.EAST); pack(); setVisible(true); } public void setBalance(int accountNumber) throws IOException { String accountFile="accounts/accounts.csv"; int accountTranslation=0; boolean accountVerified=false; FileReader freader = new FileReader(accountFile); BufferedReader outputFile = new BufferedReader(freader); String str="404"; while((str!=null)&&(accountVerified!=true)) { str = outputFile.readLine(); if ((str!=null) || (accountTranslation==accountNumber)) { String[] tokens = str.split(","); accountTranslation = Integer.parseInt(tokens[0]); if(accountTranslation==accountNumber) { balance=Double.parseDouble(tokens[1]);//here is where to find balance accountVerified=true; } else { accountVerified=false; } } } if(accountVerified==false) JOptionPane.showMessageDialog(null, "Account does not exist"); } //this method returns balance public double getBalance() { return balance; } }
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; public class AccountBalancePanel extends JPanel { //private double b=atmEngine.getBalance(); public boolean withdrawDeposit = true; private JLabel balance; public AccountBalancePanel() { balance = new JLabel("Account Balance: $0.00"); setBorder(BorderFactory.createTitledBorder("Current Information")); add(balance); } //below is the constructor that accepts the balance public AccountBalancePanel(double accBalance) { balance = new JLabel("Account Balance: $"+accBalance); System.out.println(accBalance); setBorder(BorderFactory.createTitledBorder("Current Information")); add(balance); } }
•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
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:
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.
Btw, to further explain what others have said, here is what you want to do:
Java Syntax (Toggle Plain Text)
If you want class ATM to be able to update class BankGUI, then the constructor for ATM would look like this: public ATM(BankGUI theGUI, ....){ atmsBankGUI = theGUI; //ATM has to have a variable called atmsBankGUI } And lets say ATM lets you withdraw money. public void withdraw(){ int amount = dollarAmount; //get the dollar amount the user wants to withdraw atmsBankGUI.withDrawCash(amount); } //Now, this method would be in the BankGUI class withDrawCash(){ //Do whatever you have to, to make the GUI display the withdrawal //For example, you could use JLabel.setValue to set it to the new amount of money the user has. }
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.
![]() |
Similar Threads
- How can I access methods or attributes from a subclass (Java)
- String class methods... (Java)
- Viewing java classes (Java)
- Newbi can anyone help with methods and classes (Java)
- Where to get started with Web Programming (IT Professionals' Lounge)
- storing references to classes (C++)
- Working with objects of different Classes (Java)
- Need help with Const, Ref and Classes. (C)
Other Threads in the Java Forum
- Previous Thread: Java coding problem
- Next Thread: Receiving Sms from mobile through Gateway using java..
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






