TransWikia.com

Java writing unlimited text lines to a UI

Stack Overflow Asked by Barry Griffey on February 11, 2021

I have a program that writes text generated within the program to the UI. I’ve tried JTextArea and successfully output the number of lines equal to the number of rows in the object (currently set at 10 rows). However, within my program there can be hundreds of lines generated that need to be sent to the UI, and each line after the 10th one is not written (at least where I can see it in the UI). It is not necessary to continually display every line, but rather displaying the most recent N lines is acceptable.

Below is the code used to create the textarea called programOutputText and add to a frame.

programOutputText = new JTextArea();
programOutputText.setRows(10);
programOutputText.setColumns(135);
add(programOutputText);

Below is the code that is executed each time a new line is written.

programOutputText.append("constant string  " + programGeneratedString + "n");
programOutputText.selectAll();
programOutputText.paintImmediately(programOutputText.getVisibleRect());   

Again this works for the first 10 writes (rows = 10) but then does not work afterwards. If I could find a way to manually control the latest lines written by manually moving rows down to make room for (and insert) the latest/newest row, that is acceptable.

I also tried embedding the JTextArea within a JScrollPane, but the result was exactly the same.

I’m not tied to the idea of using a JTextArea, but that’s the one that seemed to be the best fit, if I can figure out how to use it to meet my requirements.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP