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:

public double getBalance()
    {
        return balance;
    }

How do i access this get method from one of the panel classes so I can update the GUI? I tried adding:

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 :P)

Thank so much,
PO.

Recommended Answers

All 4 Replies

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.

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.

balance = JFrame.getInstance().getBalance();

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:

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;
    }
}
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);
    }
}

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 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.