Question about GTextArea.setText() in G4P

I have a question about GTextArea.setText(). I wrote a function that loads some lines from a text file and then uses GTextArea.setText() to load the lines into a text area. It seemed to be skipping empty lines, and at first I thought it was the loadStrings() function that was skipping them. But it appears that GTextArea.setText() is skipping the blank lines. Is it supposed to do that?

Here’s my code:

  public void loadHelp() {
    File file = null;
    int help = this.helpList.getSelectedIndex();
    file = new File(dataPath(fileList[help]));
    if (file.isFile()) {
      String[] lines = loadStrings(file);
      for (int i=0; i<lines.length; i++) {
        println(i,lines[i]);
      }
      this.helpText.setText(lines);
      this.message.setText("");
    } else {
      this.helpText.setText(descList);
      this.message.setText("Help file not available.");
    }
  }

It displays the following messages in the console log:

0 Var Delay : variable length delay
1 
2 CV 1 : delay amount in ms. (2 - 5000)
3 
4 CV 2 : feedback amount (0 - 100%)

But as you can see in the screen print, the blank lines are missing.

Thanks again.
setText

1 Like

P.S. I got around the problem by padding each line with an extra space before calling GTextArea.setText(). So it seems that it skips lines of zero length ("") but not lines with at least one character, even a space, such as " ".

1 Like

If you try the G4P_TextAreaControl example you will see the this control supports multiple styles for part or all of the text. It does this using the G4P library class StyledString which gives you great control over the text appearance. This class is heavily dependent on some low level Java classes and the only limitation is when editing the text in GTextArea the text insertion point generates an exception if it is on an empty line (length == 0). The only workaround I could find was to strip away empty lines when using setText(...) or to use a single space.

3 Likes

Thanks. Might be an idea to add a note to the documentation then.

1 Like