2

I would like to know if it's possible to support '\r' (carriage return) without new line break in JTextArea or is this feature supported in any other similar implementations in Java?

Say, after several text appended to a text area, I want this to be appended.

"Please wait, a download is in progress"

After that a download progress status with progress meter character '#' gets appended. e.g.:

#\r
##\r
###\r
####\r
#####\r

I would like to inform here that, I'm reading the progress from a buffer character after character. I don't want to replace the '\r' characters I'm reading from the buffer in any way.

So, what I would love to see if, the above progress, is displayed in a single line, without having a line break Say, #########

Best regards

SSaikia_JtheRocker
  • 5,053
  • 1
  • 22
  • 41

2 Answers2

3

As an alternative to marking progress in the JTextArea itself, consider decorating it with a JProgressBar or a variation.

Addendum: SwingWorker is useful for managing lengthy background tasks while displaying interim progress. In this example, a worker iterates through a SQL ResultSet and updates a JProgressBar. Note that the GUI's PropertyChangeListener can update arbitrary components, and the progress bar can be customized.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Hi Trashgod, I think I didn't make myself very clear. The thing is several chars are being read from the buffer. I cannot put all the characters in a JProgressBar setProgress(). Only some part(characters) of the whole buffer have that #\r ##\r ###\r ####\r #####\r So if line break doensn't occur where there is '\r', it would mean the whole progress would look like progressing in the same line... like #####, no line breaks in between. I'm ready to explain more if required. – SSaikia_JtheRocker Mar 30 '12 at 06:19
  • I'm advocating a clear separation between the text component's content and any indication of progress. I don't see a way to both _change_ the content and _not change_ the content at the same time. – trashgod Mar 30 '12 at 13:59
  • Hi Trashgod, by not able to change the buffer I mean, let's say I have the following content coming from the buffer, 'Download is in progress...**\r\n**Please wait...**\r\n**#\r ##\r ###\r ####\r #####\r\nDownload complete...'. So, how would I deal with this problem if I was to modify the characters coming my way. You got it ,right? **\r** can be anywhere other than my point of interest. – SSaikia_JtheRocker Mar 30 '12 at 14:25
  • 1
    That's not how I'd do it. It looks like you want to simulate the appearance (or use the output) of a command line utility like `curl`. @MKorbel's [suggestion](http://stackoverflow.com/a/9931536/230513) seems reasonable. – trashgod Mar 30 '12 at 14:47
  • Trashgod: Yes correct :) So do you have any other better solution to this problem? So if you get this type of problem what would you do? – SSaikia_JtheRocker Mar 30 '12 at 16:50
  • @JtheRocker: I've elaborate above. – trashgod Mar 30 '12 at 17:06
  • Trashgod: Thanks, will look and get back to you :) – SSaikia_JtheRocker Mar 30 '12 at 18:15
2

You have look at public void read(Reader in, Object desc) throws IOException

mKorbel
  • 109,525
  • 20
  • 134
  • 319