I'm creating a GUI in Java (Netbeans) and I want to create a multi-agent system on it. The GUI will have some Text Area and a Button, so I want to click that button and give all the text information to an agent to process but I don“t know how communicate the GUI with the Agents or the AgentContainer.
GUI Class (main):
public class GUI extends javax.swing.JFrame {
Contenedor c;
public GUI() {
initComponents();
c = new Contenedor();
c.contenedor();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
txtArea = new javax.swing.JTextArea();
btnDesplegar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
txtArea.setColumns(20);
txtArea.setRows(5);
jScrollPane1.setViewportView(txtArea);
btnDesplegar.setText("Desplegar");
btnDesplegar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDesplegarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(17, 17, 17)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38)
.addComponent(btnDesplegar)
.addContainerGap(47, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(82, 82, 82)
.addComponent(btnDesplegar)))
.addContainerGap(171, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnDesplegarActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(txtArea.getText());
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnDesplegar;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea txtArea;
// End of variables declaration
}
AgentContainer:
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentContainer;
import jade.wrapper.StaleProxyException;
import java.util.logging.Level;
public class Contenedor {
private AgentContainer contenedorAgentes;
public void contenedor(){
Runtime runtime = Runtime.instance();
Profile profile = new ProfileImpl(null, 1097, null);
contenedorAgentes = runtime.createMainContainer(profile);
agregarAgentes();
}
private void agregarAgentes(){
try {
contenedorAgentes.createNewAgent("Ag1", Ag1.class.getName(), null).start();
}
catch (StaleProxyException ex) {
java.util.logging.Logger.getLogger(Contenedor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Agent: import jade.core.Agent; import jade.core.behaviours.CyclicBehaviour;
public class Ag1 extends Agent{
@Override
public void setup(){
addBehaviour(new Comportamiento());
}
@Override
public void takeDown(){
}
class Comportamiento extends CyclicBehaviour{
@Override
public void action() {
System.out.println("pan");
}
}
}
We lack some information. As agents are threads, to give them the data collected with the GUI will vary depending of your design choices.