I work with java swing . The monitor displays data with the variable newDegree (thanks to Thread, different data is converted every 5 seconds). When the jButton1 button jButton1 pressed, the data should also be constantly changing, but already be displayed in the Fahrenheit format far . While it was possible only to do the following: when the button is pressed, the far value is displayed once. But then again, newDegree . The task is to display far values ​​until quietly until the program is closed or another button is pressed.

In fact, the program simply displays temperature changes in Celsius and must, when the button is pressed, produce constantly Fahrenheit data.

  private void heatSensor1HeatChanged(system.HeatChangedEvent evt) { double newDegree = evt.getCelsius(); String date = evt.getDate(); monitor1.setText(String.valueOf(newDegree)); far = newDegree; far = 9*far/5 + 32; System.out.println("F============="); System.out.println(far); System.out.println("F============="); System.out.println("C============="); System.out.println(newDegree); System.out.println("C============="); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { monitor1.setText(String.valueOf(far)); // TODO add your handling code here: } 

    1 answer 1

    I would do as follows.

    1. I would add an additional private boolean-variable isTemperatureInCelcium (if true, the temperature will be displayed in Celsius, if false in Fahrenheit).
    2. In the jButton1ActionPerformed method, by clicking on the button, the value of this variable simply changes.

       if (isTemperatureInCelcium) { isTemperatureInCelcium = false; } else { isTemperatureInCelcium = true; } 
    3. In the heatSensor1HeatChanged method, which, as I understand it, it runs every few seconds just to perform a check of the following kind and change the text on the monitor depending on the value of the variable.

       if (isTemperatureInCelcium) { monitor1.setText(String.valueOf(newDegree)); } else { monitor1.setText(String.valueOf(far)); } 
    • Thanks, it works perfectly. And why did I not think of such a thing myself ... - Alex