Create/Improve 使用绑定的伪终端

Create/Improve a pseudo-terminal using bindings

我的目标是创建一个伪终端,它将显示我收到的数据。为了创建我的伪终端,我使用绑定到 StringPropertyTextArea。当我的 StringProperty 属性更改时,我的 TextArea 必须更新其内容。

我实现了一个第一个版本,它显示了我收到的每条消息,但是TextArea 的内容在我每次收到新数据时都会被删除.

版本 1:

In the class that receives the data :

StringProperty sendData = new SimpleStringProperty();
WHEN I RECEIVE DATA : sendData.set(Arrays.toString(strings));

In the class that contains the TextArea :

@FXML private TextArea consoleTextArea;
public void setLiaison (StringProperty textRecu){
        consoleTextArea.textProperty().bind(textRecu);
}

In a third class I initialise everything, and call :

Controller.setLiaison(sendData);

不希望我的文本区域每次都被清除我当然会收到数据,所以我试试这个:

consoleTextArea.textProperty().bind(Bindings.concat(consoleTextArea.getText()).concat("\n").concat(textRecu.get()));

但这根本不起作用,它只显示我在 TextArea 上收到的第一条消息,然后什么都没有。

是否有另一种方法可以使用绑定保留我的 TextArea 中的内容?

NB : 我不能使用 简单方法 就像追加文本一样,因为我使用 MVC,而我的控制器(TextArea) 必须是 link 我的模型 (StringProperty).

要么StringProperty存储所有连接在一起的消息(而不是您当前使用的sendData.set(...)方法):

sendData.set(sendData.get() + Arrays.toString(strings));

每次 属性 更改时通过串联更新文本区域(而不是您当前拥有的绑定):

textRecu.addListener((obs, oldText, newText) -> consoleTextArea.appendText(newText));

对于这两个版本中的任何一个,如果输入大量文本,性能最终都会受到影响(因为字符串连接基本上是一个缓慢的过程)。例如,使用 ObservableList<String> 来保存所有消息,并使用 ListView<String> 来显示它们可能会更好,如果列表开始变得太满,则删除较早的消息。