Anna L
Anna L

Reputation: 5

How do I get information of the variables from one class to another Action Listener class?

This is my code for the two file classes. What do I have to add/fix in order for the variables to transfer and print in the action listener JFrame? The error "No field named "(the variable I used in action listener from main class)" was found in type "Next"

  import java.awt.*; //for Dimension
  import javax.swing.*; //for GUI components

  public class MortgageCalculator
  {
   public static void main (String[] args)
    {
    JFrame frame = new JFrame ();
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.setSize (new Dimension (300, 250));
    frame.setTitle ("Mortgage Calculator");

    frame.getContentPane ().setLayout (new BorderLayout ());

    frame.getContentPane ().add (new JLabel ("                          Personal Information"), BorderLayout.NORTH);

    JPanel centerPanel = new JPanel (new GridLayout (15, 1));
    centerPanel.add (new JLabel ("Full Name:"));
    JTextField nameField = new JTextField ();
    centerPanel.add (nameField);
    centerPanel.add (new JLabel ("Your Age:"));
    JTextField ageField = new JTextField ();
    centerPanel.add (ageField);
    centerPanel.add (new JLabel ("Yearly Income:"));
    JTextField incomeField = new JTextField ();
    centerPanel.add (incomeField);
    centerPanel.add (new JLabel ());
    centerPanel.add (new JLabel ("                          Mortgage Information"));

    centerPanel.add (new JLabel ("Price of Property:"));
    JTextField priceField = new JTextField ();
    centerPanel.add (priceField);
    centerPanel.add (new JLabel ("Down Payment:"));
    JTextField downPayField = new JTextField ();
    centerPanel.add (downPayField);
    centerPanel.add (new JLabel ("Interest Rate:"));
    JTextField interestRateField = new JTextField ();
    centerPanel.add (interestRateField);
    centerPanel.add (new JLabel ("Amortization Period (Years):"));
    frame.getContentPane ().add (centerPanel, BorderLayout.CENTER);

    JPanel southPanel = new JPanel (new GridLayout (5, 2));
    JButton amort20 = new JButton ("20");
    southPanel.add (amort20);
    JButton amort25 = new JButton ("25");
    southPanel.add (amort25);
    JButton amort30 = new JButton ("30");
    southPanel.add (amort30);
    southPanel.add (new JLabel ("Payment Option:"));
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    JButton paymentWeek = new JButton ("Weekly");
    southPanel.add (paymentWeek);
    JButton paymentBiweek = new JButton ("Biweekly");
    southPanel.add (paymentBiweek);
    JButton paymentMonth = new JButton ("Monthly");
    southPanel.add (paymentMonth);
    //To add a blank line in frame, added three blank JLabels because it is 3 horiontal for grid layout
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    southPanel.add (new JButton ("Clear Information"));
    southPanel.add (new JLabel ());
    JButton nextButton = new JButton ("Next");
    southPanel.add (nextButton);
    frame.getContentPane ().add (southPanel, BorderLayout.SOUTH);


    frame.pack ();
    frame.setVisible (true);
    nextButton.addActionListener (new Next ());

    //Gets the personal information from the text fields
    int age, income, price, downPay, interestRate;
    String name = nameField.getText ();
    String ageText = ageField.getText ();
    age = Integer.parseInt (ageText);
    String incomeText = incomeField.getText ();
    income = Integer.parseInt (ageText);
    //Gets the mortgage information from the text fields
    String priceText = priceField.getText ();
    price = Integer.parseInt (ageText);
    String downPayText = downPayField.getText ();
    downPay = Integer.parseInt (ageText);
    String interestRateText = interestRateField.getText ();
    interestRate = Integer.parseInt (interestRateText);

 }
}

The ActionListener:

import java.awt.*; //for Dimension
import javax.swing.*; //for GUI components
import java.awt.event.*; //for MessageListener

public class Next implements ActionListener
{
 public void actionPerformed (ActionEvent event)
 {
    //Output frame
    JFrame frame2 = new JFrame ();
    frame2.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame2.setSize (new Dimension (300, 250));
    frame2.setTitle ("Mortgage Calculator");
    frame2.getContentPane ().setLayout (new BorderLayout ());

    JPanel westPanel2 = new JPanel (new GridLayout (3, 1));
    JLabel nameLabel = new JLabel ("Name: " + name);
    westPanel2.add (nameLabel);
    JLabel ageLabel = new JLabel ("Age: " + age);
    westPanel2.add (ageLabel);
    JLabel incomeLabel = new JLabel ("Yearly Income: " + income);
    westPanel2.add (incomeLabel);
    frame2.getContentPane ().add (westPanel2, BorderLayout.WEST);

    JPanel centerPanel2 = new JPanel (new GridLayout (3, 1));
    JLabel priceLabel = new JLabel ("    Price of property: " + price);
    centerPanel2.add (priceLabel);
    JLabel downPayLabel = new JLabel ("    Down Payment: " + downPay);
    centerPanel2.add (downPayLabel);
    JLabel interestRateLabel = new JLabel ("    Interest Rate: " + income);
    centerPanel2.add (interestRateLabel);
    frame2.getContentPane ().add (centerPanel2, BorderLayout.CENTER);

    frame2.pack ();
    frame2.setVisible (true);

  }
}

Upvotes: 0

Views: 70

Answers (1)

APerson
APerson

Reputation: 8422

The variables in your main method are local variables, so your listener won't be able to see them.

There are a lot of ways to fix this. First, you have to make the state variables (name, etc) member variables of the MortgageCalculator class, so that they'll be visible to other methods or classes. Then, give the listener code access to those variables. Some options:

  • Make Next an inner class of MortgageCalculator, so that it can automatically see the member variables.
  • Make MortgageCalculator implement ActionListener, and move the actionPerformed method inside the resulting class. Then, instead of writing nextButton.addActionListener(new Next());, you'd say nextButton.addActionListener(this); - but only inside a non-static method (so not main - you'd need to make main call a non-static method on an instance of MortgageCalculator).

Upvotes: 1

Related Questions