javafx.scene.control.TextArea.clear() 不会清除显示的文本



我有一个小型的JavaFX应用程序,其中包含一个TextArea字段来显示操作的结果。 在运行类并第二次显示结果之前,我想清理文本区域。 我使用 TextArea.clear((;

但是文本仍然被支付,并且仅在方法结束时用 TextArea.insertText(0, text( 覆盖。

eBICSResultText is TextArea

public void execute(ActionEvent event) {
eBICSResultText.clear();
eBICSResultText.setStyle("-fx-text-fill: black;");
String currentBankAccess=CBbankAccess.getValue();
String currentUser=CBuserId.getValue();
String currentOT=CBorderType.getValue();
String transferDirect=myEBICSData.getTransferDirect(currentOT);
List<String> paramList = new ArrayList<String>();
paramList.add(currentUser);
paramList.add(currentBankAccess);
paramList.add(currentOT);
if (transferDirect.equals("R")) {
paramList.add(1, "send");
String fileToSend=sendFile.getText();
if (fileToSend.equals("")) {
eBICSResultText.setStyle("-fx-text-fill: firebrick; -fx-highlight-text-fill: firebrick;");
eBICSResultText.insertText(0, "no file to send specified!");
return;
}
paramList.add(fileToSend);
} else if (transferDirect.equals("S")) {
paramList.add(1, "fetch");
} else {
logger.error("no EBICS run!");
return;
}
String[] EBICSparams = paramList.toArray(new String[0]);
EBICSKernel kernel = new EBICSKernel();
kernel.process(EBICSparams);
int lastResult=kernel.getLastEBICSResult();
String lastText=kernel.getLastEBICSReturnText();
if (lastResult == -1 )
eBICSResultText.setStyle("-fx-text-fill: firebrick; -fx-highlight-text-fill: firebrick;");
eBICSResultText.insertText(0, lastText);
}

首先,我会检查以确保您清除TextArea是正确的。我曾经两次设置对文本区域的引用,而不是将它们设置为不同的值。

其次,JavaFX 事件应该在Platform.runLater(event)调用中运行。文本区域可能未清除,因为您不在 JavaFX 线程上。如果您已经(单击一个按钮,调用了来自Node的侦听器(,请忽略此表扬。

其次,如果您确定它是正确的文本区域,并且在 JavaFX 应用程序线程上,请尝试使用textArea.setText(text)

所以我去把你的函数变成了一个可运行的例子,它对我来说很好用。为了让它工作,我硬编码了你的值,并注释掉了给我错误的东西,比如我没有的类(对我来说更容易,随意改变它(

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;

public class Main extends Application {
TextArea eBICSResultText = new TextArea("Lorem ipsum dolor sit amet");
@Override
public void start(Stage stage) {
HBox hBox = new HBox();
Button executeButton = new Button("Execute");
executeButton.setOnAction(this::execute);
hBox.getChildren().addAll(eBICSResultText,executeButton);
Scene scene = new Scene(hBox);
stage = new Stage();
stage.setScene(scene);
stage.show();
}
public void execute(ActionEvent event) {
eBICSResultText.clear();
eBICSResultText.setStyle("-fx-text-fill: black;");
String currentBankAccess="123";//CBbankAccess.getValue();
String currentUser="456";//CBuserId.getValue();
String currentOT="789";//CBorderType.getValue();
String transferDirect="S";//myEBICSData.getTransferDirect(currentOT);
List<String> paramList = new ArrayList<String>();
paramList.add(currentUser);
paramList.add(currentBankAccess);
paramList.add(currentOT);
if (transferDirect.equals("R")) {
paramList.add(1, "send");
String fileToSend="";//sendFile.getText();
if (fileToSend.equals("")) {
eBICSResultText.setStyle("-fx-text-fill: firebrick; -fx-highlight-text-fill: firebrick;");
eBICSResultText.insertText(0, "no file to send specified!");
return;
}
paramList.add(fileToSend);
} else if (transferDirect.equals("S")) {
paramList.add(1, "fetch");
} else {
//            logger.error("no EBICS run!");
return;
}
//        String[] EBICSparams = paramList.toArray(new String[0]);
//        EBICSKernel kernel = new EBICSKernel();
//        kernel.process(EBICSparams);
int lastResult=1;//kernel.getLastEBICSResult();
String lastText="look at that";//kernel.getLastEBICSReturnText();
if (lastResult == -1 )
eBICSResultText.setStyle("-fx-text-fill: firebrick; -fx-highlight-text-fill: firebrick;");
eBICSResultText.insertText(0, lastText);
}

public static void main(String[] args) { launch(args); }
}

仍然不确定问题是什么,您想在函数的开头和结尾之间看到清晰的文本吗?

正如Zephyr所提到的,解决方案是将实际处理移动到一个继承javafx.concurrent.Service的单独类中。 与此类的数据交换必须通过 getter、setter 和事件来实现。

public void execute(ActionEvent event) {     
logger.entry();
eBICSResultText.setStyle("-fx-text-fill: black; -fx-highlight-text-fill: firebrick;");
eBICSResultText.clear();
eBICSResultText.setText("Process EBICS...");
String currentBankAccess=CBbankAccess.getValue();
String currentUser=CBuserId.getValue();
String currentOT=CBorderType.getValue();
String transferDirect=myEBICSData.getTransferDirect(currentOT);
List<String> paramList = new ArrayList<String>();
paramList.add(currentUser);
paramList.add(currentBankAccess);
paramList.add(currentOT);
if (transferDirect.equals("R")) {
paramList.add(1, "send");
String fileToSend=sendFile.getText();
if (fileToSend.equals("")) {
eBICSResultText.setStyle("-fx-text-fill: firebrick; -fx-highlight-text-fill: firebrick;");
eBICSResultText.setText("no file to send specified!");
return;
}
paramList.add(fileToSend);
} else if (transferDirect.equals("S")) {
paramList.add(1, "fetch");
} else {
logger.error("no EBICS run!");
}
BTexecute.setDisable(true);
MyEBICSService service = new MyEBICSService();
service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
BTexecute.setDisable(false);
eBICSResultText.setText("EBICS communcation finished");
int lastResult=service.getLastEBICSResult();
String lastText=t.getSource().getValue().toString();
if (lastResult == -1 ) {
eBICSResultText.setStyle("-fx-text-fill: firebrick; -fx-highlight-text-fill: firebrick;");
}
eBICSResultText.setText(lastText);
}
});
service.setParams(paramList);
service.start();
}
...
class MyEBICSService extends Service<String> {
/** holds the logger */
private XLogger logger = XLoggerFactory.getXLogger(MyEBICSService.class.getName());
List<String> paramList = new ArrayList<String>();
int lastResult = -1;
String lastText = "unknown";
@Override
protected Task<String> createTask() {
return new Task<String>() {
@Override
protected String call() throws Exception {
String[] EBICSparams = paramList.toArray(new String[0]);
EBICSKernel kernel = new EBICSKernel();
kernel.process(EBICSparams);
logger.debug("EBICSKernel finished");
lastResult=kernel.getLastEBICSResult();
lastText=kernel.getLastEBICSReturnText();
return lastText;
}
};
}
public final void setParams(List<String> paramList2) {
this.paramList=paramList2;
}
public Integer getLastEBICSResult() {
return this.lastResult;
}
}

最新更新