chis54
chis54

Reputation: 477

Java SWT make a Label Scrollable

I have a Label in a Group in SWT and if it contains many lines of text, I would like to make it scrollable vertically. Setting the style parameter with SWT.V_SCROLL doesn't seem to do it. How can I do this?

Upvotes: 3

Views: 1664

Answers (2)

Big Guy
Big Guy

Reputation: 334

Here is a way

ScrolledComposite ivScrollComposite = new ScrolledComposite( ivShell, SWT.V_SCROLL );
ivScrollComposite.addControlListener( new ScrollCompositeControlListener() );
ivScrollComposite.setExpandVertical( true );
ivScrollComposite.setExpandHorizontal( true );
ivScrollComposite.setAlwaysShowScrollBars( true );

Composite ivCompositeResults = new Composite( ivScrollComposite, SWT.NONE );
Label ivLabelResults = new Label( ivCompositeResults, SWT.WRAP );

ivScrollComposite.setContent( ivCompositeResults );

ivScrollComposite.setLayout( new FormLayout() );

FormData formData = new FormData();
formData.top = new FormAttachment( 0, 0 );
formData.left = new FormAttachment( 0, 0 );
formData.right = new FormAttachment( 100, 0 );
formData.bottom = new FormAttachment( 100, 0 );
ivCompositeResults.setLayoutData( formData );

ivCompositeResults.setLayout( new FormLayout() );

formData = new FormData();
formData.top = new FormAttachment( 0, 0 );
formData.left = new FormAttachment( 0, 0 );
formData.right = new FormAttachment( 100, 0 );
formData.bottom = new FormAttachment( 100, 0 );
ivLabelResults.setLayoutData( formData );

then

private void resizeScroll()
{
    Rectangle r = ivScrollComposite.getClientArea();
    ivScrollComposite.setMinSize( ivCompositeResults.computeSize( r.width, SWT.DEFAULT ) );
}

private class ScrollCompositeControlListener extends ControlAdapter
{
    @Override
    public void controlResized( ControlEvent e )
    {
        resizeScroll(); 
    }
}

And whenever you change the Label text

ivLabelResults.setText( "some text" );
resizeScroll();

Upvotes: 0

greg-449
greg-449

Reputation: 111142

Label does not support scrolling.

You could use a read only Text control which will scroll:

Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL);

Upvotes: 8

Related Questions