0

I have an Arraylist AList which has over 100000 items. I need to show each and every data item in the UI. But TeaxtArea.addtext(AList); shows data in this manner

[A,B,C,D..........]

likewise. What I need to do is to print this in ta a page. So I have to pass the node tho the PrinterJob.

Is there Anyway that I can remove the commas and replace it with newline all at once. I can not use appendText hence appending 100000 items make the UI non respond.

PS: I have already collected the data From a Thread and Have it on an ArrayList. I do not have a problem in updating it on a ListView, But there is a issue when did it on a TextArea. I wont to change the format of printing without using Append

Anjula Ranasinghe
  • 584
  • 1
  • 9
  • 24
  • Possible duplicate of [JavaFx - Updating GUI](http://stackoverflow.com/questions/26554814/javafx-updating-gui) – fabian Jan 25 '16 at 12:13

2 Answers2

0

You could preprocess the list and create a new String using StringBuilder like this:

List<String> list = new ArrayList<>();

for( int i=0; i < 100000; i++) {
    list.add("This is line nr " + i);
}

StringBuilder sb = new StringBuilder();
for( String text: list) {

    if( sb.length() > 0) {
        sb.append("\n");
    }

    sb.append(text);
}

textArea.appendText(sb.toString());

Setting the initial capacity might also help in case of such a large collection.

If you use Java 8, you may also want to take a look at StringJoiner.

Roland
  • 18,114
  • 12
  • 62
  • 93
  • Yes this method Kind of works But It makes my UI Unresponsive due to the large set of Data. This does not happens with the `ListView` – Anjula Ranasinghe Jan 25 '16 at 05:43
  • A TextArea is different to a ListView. The ListView is optimized for rendering only the parts you see whereas the TextArea renders all at once. – Roland Jan 25 '16 at 06:37
  • Any Suggestions for me how to improve this? My UI gets stuck when render all at once. I tried Throttling before But takes a massive amount of time. – Anjula Ranasinghe Jan 25 '16 at 06:40
  • If the strings are already in a list, using `list.stream().collect(Collectors.joining("\n"))` is simpler than using a `StringJoiner`. – fabian Jan 25 '16 at 12:09
0

Let us consider aList as the ArrayList of String,

ArrayList<String> aList = new ArrayList<String>();
        aList.add("A");
        aList.add("B");
        aList.add("C");

Then use this code before you go to the PrinterJob,

StringBuffer listText = new StringBuffer();
for(String s : aList){
    listText.append(s+"\n");
}
System.out.println(listText);

Try this...

Venkat Prasanna
  • 692
  • 4
  • 16