Print the result of messages received as a subscriber in a JTextArea

1

I am trying to make a program for an IoT project using the MQTT protocol, my problem is to print what I receive in a JTextArea subscriber status. I tried to update the JTextArea for every time a message arrived in messageArrived () and publish it in JTextArea but I could not, I even tried to replace and reassign the content of the console, since it prints without problem, passing it to JTextArea but neither! Came a lot to this and I hope you can help me.

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    try{
        /*
        String msj = new String(message.getPayload());
        System.out.println(msj);
        String topico = topic;        
        subscribingArea.setText(msj);
        //subscribingArea.repaint();
        */
        PrintStream out = new PrintStream( new CustomOutputStream( subscribingArea ) );

        System.setOut( out );
        System.setErr( out );
        System.out.println(new String(message.getPayload()));

    }catch(Exception e){
        setTitleText( "No se pudo imprimir la cadena" );
    }

}

It is the function where the MQTT protocol messages arrive. A part I have implemented the class to replace the contents, talking about the PrintStream method.

package com.mycompany.clientefinalpc;

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;

public class CustomOutputStream extends OutputStream {
    private JTextArea textArea;

    public CustomOutputStream( JTextArea textArea ) {
        this.textArea = textArea;
    }

    @Override
    public void write(int b) throws IOException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        textArea.append(String.valueOf((char)b));

    }}

And the part of the code where I build the elements.

public void connectButton( Container contentPane ){
        setTitleText( "" );        

        //pubPanel.setLayout( new GridLayout() );
        subPanel.setBorder( new EtchedBorder() );
        pubPanel.setBorder( new EtchedBorder() ); 

        mqttComp = new JPanel( new BorderLayout() );
        mqttComp.add( subPanel, BorderLayout.SOUTH );
        mqttComp.add( pubPanel, BorderLayout.NORTH );
        mqttComp.add( subscribePanel, BorderLayout.CENTER );

        ledNoLocalClient = new LED();
        ledNoLocalClient.setRed();
        new Thread(ledNoLocalClient).start();

        ledLocalClient = new LED();
        ledLocalClient.setRed();
        new Thread(ledLocalClient).start();

        //pubPanelControl = new PubPanel(publishPanel, aMqttMgr);
        //subPanelControl = new SubPanel(subscribePanel, aMqttMgr);

        disconnect = new JButton( "Desconectar" );        
        disconnect.addActionListener(this);

        publishButton = new JButton( "Publica" );
        publishButton.addActionListener(this);

        subscribingArea = new JTextArea(15,30);
        subscribingArea.setEditable( false );
        JScrollPane scrollpane = new JScrollPane(subscribingArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollpane.setPreferredSize( new Dimension( 350,290 ) );  

        publish = new JTextField();
        publish.setPreferredSize( new Dimension( 150,30 ) );
        publish.setEditable( true );
        publish.setText( "PUBLICA ALGO" );

        JPanel _button = new JPanel();
        _button.add( new JLabel( "Local Status" ) );
        _button.add( disconnect );  

        JPanel _text = new JPanel();
        _text.add( publish );
        _text.add( publishButton );
        _text.add( new JLabel( "Remote Status" ) );
        _text.add( ledNoLocalClient );

        JPanel _area = new JPanel();
        _area.add( scrollpane );

        pubPanel.add(ledLocalClient); 
        subscribePanel.add( _area );
        pubPanel.add(_button);   
        subPanel.add(_text);  

        contentPane.add( mqttComp );
    }
    
asked by Cristhian Plaza 24.04.2016 в 23:46
source

1 answer

0

I have already managed to find an answer, coincidentally for the observers, I was executing a second frame and it had to perform some processes which I was not executing, for this I have eliminated the second frame and I have implemented everything in the first one and worked correctly, to update the frame I used only, append (), which writes me a new line every time a new message arrives.

    
answered by 04.05.2016 в 00:54