This thread is updated for a reprex (minimal reproducible code) as per requested.
I'm building a multi-agent application with JADE for the first time, and I'm using NetBeans GUI Builder (Java Swing) for that.
In short there are three classes (MainContainer.java, ClientContainer.java and ClientAgent.java) and one library (jade.jar).
After launch, I seem to send data from the Agent to the Container normally (I can print the values in the console, Container side), however, I cannot:
statusArea.append(message)
statusArea.setText(message)
on the JTextArea (statusArea) when I invoke the method viewMessage(String message)
from the Agent Class for some reason, I even tried using a SwingWorker.
can print on console, but not on the GUI
Here's a minimal reproducible code of what I'm trying to achieve:
ClientAgent.java:
public class ClientAgent {
public ClientAgent() {
// Instance of the client container
ClientContainer gui = new ClientContainer();
// Setting this agent to it's container
gui.setClientAgent(this);
// Sending data to GUI container through viewMessage(String str) method
gui.viewMessage("-- Client init");
gui.viewMessage("Scope: " + "AID");
}
public static void main(String[] args) throws InterruptedException {
ClientAgent obj = new ClientAgent();
}}
ClientContainer.java:
import javax.swing.SwingWorker;
public class ClientContainer extends javax.swing.JFrame {
ClientAgent clientAgent;
public ClientContainer() {
initComponents();
}
public ClientAgent getClientAgent() {
return clientAgent;
}
// Setter to set this container as the UI for the agent
public void setClientAgent(ClientAgent clientAgent) {
this.clientAgent = clientAgent;
}
// Method that displays data on statusArea from ClientAgent.java class
public void viewMessage(String message) {
SwingWorker<Void, String> Worker = new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(550);
System.out.println(message);
statusArea.append(message + "\n");
return null;
}
};
Worker.execute();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
statusArea = new javax.swing.JTextArea();
setTitle("Client");
statusArea.setEditable(false);
statusArea.setColumns(20);
statusArea.setRows(5);
jScrollPane2.setViewportView(statusArea);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Flat Light".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ClientContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// Starting the client container
new ClientContainer().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea statusArea;
// End of variables declaration }
In hopes that no body that compiles this code will face any trouble, I compiled it on my machine beforehand.
Thanks again.
[EDIT]: After applying Abra's suggestion I added this to my SwingWorker inside the viewMessage method in the ClientContainer
public void viewMessage(String message) {
SwingWorker<Void, String> Worker = new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
return null;
}
@Override
protected void done() {
System.out.println("Inside done function");
System.out.println(message);
statusArea.setText(message + "\n");
}
};
Worker.execute();
}
Then I started an instance of ClientAgent in a jButton like this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
clientAgent = new ClientAgent();
}
Started the ClientContainer then the ClientAgent to see what happens, still prints on the console and not on the JTextArea.