Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append Textarea result from another class

Tags:

java

swing

I have two classes class Game and class wampusGUI. In wampusGUI class I have one textarea named displayTextArea under the method textarea1(). I am trying to append result to textarea from Game class. but when I am trying to access from that class . the function running fine and also the result is coming in that class (I just tested by simply System.out.print() method), but it is not appending to textarea. Here is my code.

// Code of wampusGUI  class
public class wampusGUI extends javax.swing.JFrame {

    /**
     * Creates new form wampusGUI
     */
    public wampusGUI() {
        initComponents();
    }

    public void textArea1(String text) {
        System.out.print(text);
        displayTextArea.append(text); // this is not appending to textarea.
    }

           /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                new wampusGUI().setVisible(true);
                   Game g = new Game();
                   g.testing();
            }
        });
    }

//Here is the code of Game class

      private wampusGUI gui;

      public void testing () {         
          String welCome=welcome();
          gui= new wampusGUI();
          gui.textArea1(welCome);            
     }
like image 588
RK. Avatar asked Dec 19 '25 09:12

RK.


1 Answers

Make this Changes in your code

In Your First Class wampusGUI

public class wampusGUI extends javax.swing.JFrame {

    /**
     * Creates new form wampusGUI
     */
    public wampusGUI() {
        initComponents();
    }

    public void textArea1(String text) {
        System.out.print(text);
        displayTextArea.append(text); // this is not appending to textarea.
    }

           /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                wampusGUI w=new wampusGUI();
                w.setVisible(true);
                Game g = new Game(w);
                g.testing();
            }
        });
    }

And for Second class Game

private wampusGUI gui;

//Add Contructor with Parameter

     public Game(wampusGUI w){
      //put this line of code at the end  
      gui=w;
     }

      public void testing () {         
          String welCome=welcome();          
          gui.textArea1(welCome);            
     }

this will work...

like image 53
Azuu Avatar answered Dec 20 '25 22:12

Azuu