• Post Reply Bookmark Topic Watch Topic
  • New Topic

A reliable way to redraw a frame?

 
Michael Remijan
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on custom JTextArea which will not display all of it's data but instead as the user scrolls down the data will be appended to the JTextArea in chunks. The problem I'm having is that after the user drags the scrollbar down and the next chunk of data is appended to the JTextArea the scrollbars remain in an inconsistent state as if the JScrollPane doesn't know the JTextArea has been updated and so it needs to be updated. Is there a way to reliably made everything redraw to the user won't see "another bad Java gui" which doesn't redraw properly?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextArea will automatically position the scrollBars of a parent JScrollPane if you add all
the text to it. If you append text in small amounts the vertical scrollBar will allow you to
scroll to the bottom/end of the appended text. But the textArea has no way of knowing that
you will add more text or how much room you want it to save for any possible, future
additions. It can only deal with what it has in the moment.
If you'd like to manage the text yourself you could make up a custom component with a
JLabel, a vertical JScrollBar and use the LineBreakMeasurer and TextLayout classes to set
the text in the label. The LineBreakMeasurer api has an example. Then you could set your
scrollBar according to your desire without interference from Swing.
 
Michael Remijan
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that's not exactly what I want to do. If text is added to a JTextArea you are correct in saying the ScrollBars will automatically fix themselves. If you write a little program to add a few new lines to a JTextArea everyy second this can clearly be seen. However, my problem comes when dragging the vertical scrollbar down. When I have scrolled all the way down I can capture that event with a AdjustmentListener and add more lines to the JTextArea. But the act of adding the new lines to the JTextArea does not cause the scrollbar to redraw because I am using the mouse to drag the vertical scrollbar down and that drag operation is preventing the component from being redrawn. I think what I need to do is somehow stop the drag operation, do the updates to the jtextarea, then try to restart the drag operation again. I have a small example application i'm working on if your interested.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a small example application i'm working on if your interested.
Let's have a look.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!