JavaFX textArea update

I have a simple JavaFX application that has TextArea. I can update the contents of textArea using the code below inside the start () method:

new Thread(new Runnable() { public void run() { for (int i = 0; i < 2000; i++) { Platform.runLater(new Runnable() { public void run() { txtarea.appendText("text\n"); } }); } } }).start(); 

The code simply writes the string text to TextArea 2,000 times. I want to update this textArea function from a function that is implemented outside the start () method.

 public void appendText(String p){ txtarea.appendText(p); } 

This function can be called from arbitrary programs that use the JavaFX application to update TextArea. How to do this in appendText function?

+4
source share
1 answer

You can specify a class that should write a link to your class containing the public void appendText(String p) method in public void appendText(String p) , and then just call it. I would also suggest that you pass an indication of which class the method was called from, for example:

 public class MainClass implements Initializable { @FXML private TextArea txtLoggingWindow; [...more code here...] public void appendText(String string, String string2) { txtLoggingWindow.appendText("[" + string + "] - " + string2 + "\n"); } } public class SecondClass { private MainClass main; public SecondClass(MainClass mClass) { this.main = mClass; } public void callMainAndWriteToArea() { this.main.appendText(this.getClass().getCanonicalName(), "This Text Goes To TextArea"); } } 
+5
source

Source: https://habr.com/ru/post/1500343/


All Articles