Hello,

I have created a GUI using the Netbeans GUI Builder and I have a JTextArea that I want to output to, but because I need to use threads I am doing a lot of processing in an external thread. I am having issues outputting data from the thread class to the JTextArea in my main GUI Class. Here is the most important code:

// In my MainGui class I have a public JTextArea
public javax.swing.JTextArea console;

console = new javax.swing.JTextArea();


//I also have a button on this GUI
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Thread connect = new Thread(new ConnectThread());
    connect.start();
    //ConnectThread is my external Thread Class
}       



//In ConnectThread
//It is thread so it implements Runnable
public class ConnectThread implements Runnable{

    MainGui maingui = new MainGui();

    public void run(){
        maingui.console.append("test");
        //Doesn't output to the text area!
    }

}

Any help would be appreciated

create a setTextInJTextArea and getTextInJTextArea method. or, a getJTextArea, but to keep control of that textarea in the containing class, I suggest the first.

I added a method called setTextArea in my main gui class and it looks like this:

public void setTextArea(String text){
    console.append(text);
}

I can call it from the MainGui class perfectly fine, but calling it from an external class doesn't work?

sure it does.
either you need to have that class instantiated in that other class, or you need to pass a reference to the 'this' object of that class with the frame, in the class where you try to set the text (check out the Singleton pattern for an example)
or you'll need to have that method static, which 'll lead to a bit the same thing, since you won't be able to deal with a non-static variable in a static method, without being able to inform the JVM of which instance you're talking about.

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.