Reputation: 3198
I am trying to pass some variable that belong to an object to another class so I can use them. I can not figure out how to do so. Please help!
here is the object
package appgui;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainFrame extends JFrame {
private BankAccount account;
private JLabel label;
private JTextField amountField;
private static final int FRAME_HEIGHT = 600;
private static final int FRAME_WIDTH = 400;
public MainFrame(String t, BankAccount anAccount) {
super(t);
account = anAccount;
// Label that displays the result
label = new JLabel("Balance: " + account.getBalance());
label.setFont(new Font("Arial", Font.BOLD, 22));
label.setForeground(Color.RED);
// Create the label, text field, and button for entering amount
JLabel amountLabel = new JLabel("Amount: ");
amountField = new JTextField(7);
// Create the Control Panel that holds all components
JPanel controlPanel = new JPanel();
controlPanel.add(amountLabel);
controlPanel.add(amountField);
controlPanel.add(createWithdrawButton());
controlPanel.add(createDepositeButton());
controlPanel.add(label);
add(controlPanel);
setSize(FRAME_HEIGHT, FRAME_WIDTH);
//setLayout(new GridBagLayout());
}
private JButton createDepositeButton() {
JButton depositButton = new JButton("Deposit");
ActionListener listener1 = new DepositListener();
depositButton.addActionListener(listener1);
return depositButton;
}
and here is the class I am trying to pass some of the variables above into because I need their values like the amountField
, account
, and label
package appgui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
class DepositListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double depositAmount = Double.parseDouble(amountField.getText());
account.deposit(depositAmount);
label.setText("Balance: " + account.getBalance());
}
}
Upvotes: 1
Views: 86
Reputation: 425238
Create an interface for the fields the other class is interested in, have the object's class implement it, and have the other class's methods expect an instance of the interface:
public interface AccountDisplay {
JLabel getAmount();
BankAccount getAccount();
JTextField getLabel();
}
public class MainFrame extends JFrame implements AccountDisplay {
// implement the getters of the interface to return the appropriate fields
}
class DepositListener implements ActionListener {
private AccountDisplay accountDisplay;
public DepositListener(AccountDisplay accountDisplay) {
this.accountDisplay = accountDisplay;
}
public void actionPerformed(ActionEvent event) {
double depositAmount = Double.parseDouble(accountDisplay.getAmount().getText());
accountDisplay.getAccount().setBalanceText(....);
}
}
Upvotes: 1
Reputation: 285405
You don't want to get "part of an object", but rather get the object itself, and query it for its state via getter methods as well as set its state via setter methods.
Your listener needs a reference to the GUI that calls it. Cleanest I think is to just pass it in in a constructor parameter:
class DepositListener implements ActionListener {
private MainFrame mainFrame;
public DepositListener(MainFrame mainFrame) {
this.mainFrame = mainFrame;
}
public void actionPerformed(ActionEvent event) {
// you'll need to give MainFrame public methods to extract information, e.g.,
double depositAmount = Double.parseDouble(mainFrame.getAmount());
// ... etccc
// and to display information
mainFrame.setBalanceText(....);
}
}
To riff on Bohemian's answer (1+ to it), yes, use interfaces, but I don't like getting the actual components but rather just the necessary info. Expose only what is needed to be exposed:
public interface AccountDisplay {
double getAmount();
BankAccount getAccount(); // this is OK if BankAccount is non GUI
void setBalanceText(String text);
}
Upvotes: 1
Reputation: 142
Just create getters for the variables that you're trying to access, and have your second class hold a object reference to the first class
Upvotes: 0
Reputation: 17007
Well, how about an inner class:
public class MainFrame extends JFrame {
....
class DepositListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double depositAmount = Double.parseDouble(amountField.getText());
account.deposit(depositAmount);
label.setText("Balance: " + account.getBalance());
}
}
}
That will work.
Upvotes: 1