简体   繁体   中英

How to get JTextArea text from another method

I want to use method from another class to append in my JTextArea ( TEXT IS HERE in my code), how to do it?

 SecondWindow() {

    super("Mortage Loan Calculator");
    setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));

    tArea = new JTextArea(***TEXT IS HERE***, 30, 40);
    scroll = new JScrollPane(tArea);
    add(scroll);
    setLocationRelativeTo(null);`

`And here is a method:

public void calcAnnuity(int years, int months, double amount, double rate){
    double totalMonths = (12 * years) + months;
    double partOfRate = rate / 12.0 / 100.0;
    double tempAmount = amount;
    double payment = amount * partOfRate * Math.pow(1 + partOfRate, totalMonths) / (Math.pow(1 + partOfRate, totalMonths) - 1); //mathematical formula

    DecimalFormat decFormat = new DecimalFormat("#.##");

    System.out.println(1 + " Payment = " + decFormat.format(payment) + "--- Left to pay: " + decFormat.format(amount));

    for(int i = 2; i <= totalMonths; i++) {
        tempAmount -= (payment - partOfRate * amount);
        amount -= payment;
        **textishere.append**(i + " Payment = " + decFormat.format(payment) + " --- Left to pay: " + decFormat.format(tempAmount));
    }
}

Well, the easiest way would be to implement a public static method in your ClassA where the JTextArea is located.

public static setJTextAreaText(String text){
   tArea.setText(text);
}

And in your ClassB you import ClassA and then call this method from your method calcAnnuity()

import ClassA;

public void calcAnnuity(int years, int months, double amount, double rate){

   ...

   ClassA.setJTextAreaText('**textishere.append**');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM