how to force scrolling of a Jlabel ?


G

Guest

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).
 
Ad

Advertisements

R

Roedy Green

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 */
}
} );
 
G

Guest

// 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.
 
Ad

Advertisements

L

Lew

LC's NoSpam Newsreading account said:
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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top