Home > Software engineering >  Java update Label automatically every time an MQTT message arrives
Java update Label automatically every time an MQTT message arrives

Time:12-27

I am getting MQTT messages using a Java program and I am using Java swing to represent data. The problem is that I have to click a button to update jlabel in order to get new values. I am trying to do this automatically every time I get a new message.

import org.eclipse.paho.client.mqttv3.MqttException;

import javax.swing.*;
import java.awt.*;

public class MQTTCommunicationGUI {
    public MQTTCommunicationGUI() throws Exception {
        SendDataUsingMQTT mqttCommunication;
        CommChannel channel = new SerialCommChannel("/dev/cu.usbmodem14301",9600);
        try {
            mqttCommunication = new SendDataUsingMQTT();
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }

        JFrame frame=new JFrame();
        frame.setLayout(new GridLayout(3,3));

        JButton lightIntensityButton=new JButton("Light Intensity");
        JLabel lightIntensity=new JLabel("...");

        JButton pirButton=new JButton("Presence");
        JLabel pir=new JLabel("...");

        JButton on=new JButton("ON");
        JButton off=new JButton("OFF");

        frame.getContentPane().add(on);
        frame.getContentPane().add(off);
        frame.getContentPane().add(lightIntensityButton);
        frame.getContentPane().add(lightIntensity);
        frame.getContentPane().add(pirButton);
        frame.getContentPane().add(pir);

        frame.setSize(250,250);
        frame.setVisible(true);


        lightIntensityButton.addActionListener(e -> {
            lightIntensity.setText(mqttCommunication.getLightIntensity());
        });

        pirButton.addActionListener(e -> {
            pir.setText(mqttCommunication.getPir());
            channel.sendMsg(mqttCommunication.getPir());
        });

        on.addActionListener(e -> {
            try {
                mqttCommunication.publish("ON");
                frame.repaint();
            } catch (MqttException ex) {
                throw new RuntimeException(ex);
            }
        });

        off.addActionListener(e -> {
            try {
                mqttCommunication.publish("OFF");
            } catch (MqttException ex) {
                throw new RuntimeException(ex);
            }
        });


        System.out.println("Waiting Arduino for rebooting...");
        Thread.sleep(4000);
        System.out.println("Ready.");
    }
}

So in the code I have to click lightIntensityButton and pirButton in order to update lightIntensity and pir. I tried this:

Thread newThread=new Thread(() -> {
            pir.setText(mqttCommunication.getPir());
            frame.repaint();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        newThread.start();

but it is not working. Any idea?

CodePudding user response:

Perform your UI updates from the EDT. This can easily be done using invokeLater(). See also

  • Related