0

I'm writing a program to delimit data obtained through an RS-232 serial. There are three machines the program needs to receive data from, and three COM ports.

I've written some code to update a JTextArea in the programs interface to inform the user as to the port that the program is currently listening on, however the text area isn't updating based on the code and is remaining blank. The code is as such:

public String ComStat;

public void comDefine() 
{
    if (Serial.currPortId.getName() == null) {
        ComStat = "NONE";
    } else if (Serial.currPortId.getName() == "COM1") {
        ComStat = "COM1";
    } else if (Serial.currPortId.getName() == "COM2") {
        ComStat = "COM2";
    } else if (Serial.currPortId.getName() == "COM3") {
        ComStat = "COM3";
    } else {
        ComStat = "NONE";
    }
}

and for the jTextArea we have:

private JPanel createPanel(String text) {

    JPanel panel = new JPanel();
    JLabel lbl = new JLabel(text);
    panel.add(lbl);
    listenState = new JTextArea(ComStat);
    listenState.setBorder(new BevelBorder(BevelBorder.LOWERED));
    listenState.setLineWrap(true);
    listenState.setWrapStyleWord(true);
    add(listenState, BorderLayout.SOUTH);
    panel.add(listenState);

    return panel;
}

Any help on getting the JTextArea to actually display whether it's listening on a port and which port it's listening on would be much appreciated. I've cut out most of the code to make for easier reading of the area i'm actually having issue with.

Joseph Oliver
  • 169
  • 1
  • 12
  • I don't see any code which your update the text area. This is just the code which creates it. There is no code which reads from the COM ports and no code which converts the binary data to text or code which would append the text to a text area. – Aaron Digulla Nov 28 '17 at 13:42
  • Also have a look at [How do I compare strings in Java?](https://stackoverflow.com/q/513832/8097737) –  Nov 28 '17 at 13:50

2 Answers2

0

Use the string variable in jTextArea_variable.setText(string);:

listenState.setText(ComStat);
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
Abhishek S
  • 49
  • 6
  • using listenState.setText(ComStat); doesn't alter anything when I actually run the program. The text remains blank unfortunately. – Joseph Oliver Nov 28 '17 at 13:57
  • I think the issue itself, having looked into it, lies in the "comDefine" method. It never actually gets called so it never changes the value of "ComState". – Joseph Oliver Nov 28 '17 at 13:59
  • I fixed the problem! Solution was to use an if statement to address the com port code directly. – Joseph Oliver Nov 28 '17 at 14:38
0

When you have code like

new JTextArea(ComStat)

then the text area copies the current value of the variable. If you change the variable in the future, this has no effect on the JTextArea. You need to call listenState.setText(ComStat); to update the UI whenever you change the variable. Use append(String) to add more text.

Note: You must make sure that you call this in the UI thread. If you use multiple threads to read the serial ports, you must wrap the line above in SwingUtilities.invokeLater(). See https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820