Batty Batty - 7 days ago 5
Java Question

How to print to JAVAFX gui like printing to console

Trying to print data to a gui made by javafx.

...
for(Entry<Date, String> entry : tmap.entrySet()) {
Date key = entry.getKey();
String value = entry.getValue();

System.out.println(key + " => " + value);


I do not want to use
setText()
as it wont act like in console, it will clear to only one text every loop of
TreeMap

Answer

The only way to do this is by concatenating the original text with the new content and setting the resulting String using setText.

In your case you can improve the efficiency of doing this by using a StringBuilder:

StringBuilder sb = new StringBuilder(textNode.getText());

for(Entry<Date, String> entry : tmap.entrySet()) {
    Date key = entry.getKey();
    String value = entry.getValue();

    sb.append(key).append(" => ").append(value).append('\n');
    ...
}
textNode.setText(sb.toString());

In case you're using a TextInputControl, appendText may be worth a look, but in case you know you're adding multiple Strings, StringBuilder is probably more efficient.

for(Entry<Date, String> entry : tmap.entrySet()) {
    Date key = entry.getKey();
    String value = entry.getValue();

    textNode.appendText(key + " => " + value + '\n');
    ...
}
Comments