How do I force a JLabel (with HTML content) wrapped in a JScrollPane to
automatically scroll to the last line added ?
I am developing an applet whose frame is divided in three panels:
- the top one is a tabbed pane which normally contains an astronomical
image (may contain also some "control" menus, irrelevant here)
- the middle one is a JLabel wrapped in a JScrollPane
- the bottom one is a JTable also wrapped in a JScrollPane
The applet retrieves from the web the image (which is displayed in the
top panel) and a "region file" (a list of sky positions with associated
a shape, circle, box, ellipse etc. and other info). The region file is
loaded in the bottom JTable, and displayed graphically onto the top
image.
Then I have a mouse adapter which, clicking onto the image, locates the
nearest region (within a tolerance, otherwise shall issue a "no region"
message). Let's assume that the region is at index "nearest" in the
Vector of region records
RegionRecord rr=(RegionRecord) mtm.rrs.elementAt(nearest)
On a successful click, I hilight the row associated to the nearest
record in the JTable mytable. To do this I do
mytable.changeSelection(nearest,0,false,false)
(or mytable.clearSelection() if no region is located). This
automatically scrolls the Jtable up or down to put the hilighted region
row in sight.
At the same time, on each click I write a message to the middle panel,
via a method which ultimately does
msg.setText(msg.getText()+"<br>"+mymessage) ;
where msg is the Jlabel in the middle panel. This is followed by a
repaint of the overarching applet content pane.
The point is that, once the panel is full, a scrollbar appears, but the
(added) bottom line is no longer on the screen (the scroll bar has to be
scrolled manually).
I tried playing with msg.setVerticalAlignment() to TOP or BOTTOM but it
seems to have no effect once the scrollbar appears.
How do I force the JLabel wrapped in the JScrollPane to automatically
scroll to the last line added ?
(BTW the reason I used a JLabel instead of a JTextArea is in order to be
use HTML tags to format differently errors, warnings and info messages).

Signature
----------------------------------------------------------------------
nospam@mi.iasf.cnr.it is a newsreading account used by more persons to
avoid unwanted spam. Any mail returning to this address will be rejected.
Users can disclose their e-mail address in the article if they wish so.
derek - 08 Jan 2008 17:49 GMT
> How do I force a JLabel (with HTML content) wrapped in a JScrollPane to
> automatically scroll to the last line added ?
Try setting the location of the scrollbar.
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JScrollPane.html#getVertical
ScrollBar()
This is my footer.
Roedy Green - 09 Jan 2008 08:42 GMT
On Tue, 8 Jan 2008 15:37:36 +0100, LC's No-Spam Newsreading account
<nospam@mi.iasf.cnr.it> wrote, quoted or indirectly quoted someone who
said :
>How do I force a JLabel (with HTML content) wrapped in a JScrollPane to
>automatically scroll to the last line added ?
I don't know about JLabels, but in general JScrollPane works like
this:
// Make sure most recent material visible.
// G O T C H A !
// Use invokeLater to give time for text to render
// after a JTextArea.setText before we process the setValue,
// otherwise the setValue will be effectively ignored.
jTextAreaBeingScrolled.setText( newText );
SwingUtilities.invokeLater( new Runnable() {
public void run()
{
final JScrollBar v = scroller.getVerticalScrollBar();
v.setValue( v.getMaximum() ); /* Use setMinimum for making top
visible */
}
} );

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
LC's NoSpam Newsreading account - 09 Jan 2008 14:04 GMT
> // Use invokeLater to give time for text to render
Thanks ! Works like a charm ...
... and invokeLater is indeed necessary even with a JLabel, otherwise
the scrollbar position lags behind the latest text.

Signature
----------------------------------------------------------------------
nospam@mi.iasf.cnr.it is a newsreading account used by more persons to
avoid unwanted spam. Any mail returning to this address will be rejected.
Users can disclose their e-mail address in the article if they wish so.
Lew - 09 Jan 2008 15:38 GMT
>> // Use invokeLater to give time for text to render
>
> Thanks ! Works like a charm ...
>
> .... and invokeLater is indeed necessary even with a JLabel, otherwise
> the scrollbar position lags behind the latest text.
invokeLater() is necessary to move the graphics activity onto the EDT.

Signature
Lew
Roedy Green - 09 Jan 2008 10:45 GMT
On Tue, 8 Jan 2008 15:37:36 +0100, LC's No-Spam Newsreading account
<nospam@mi.iasf.cnr.it> wrote, quoted or indirectly quoted someone who
said :
>How do I force a JLabel (with HTML content) wrapped in a JScrollPane to
>automatically scroll to the last line added ?
see http://mindprod.com/jgloss/jscrollpane.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com