Skip to content

Instantly share code, notes, and snippets.

@rherrmann
Last active April 28, 2022 08:33
Show Gist options
  • Save rherrmann/b1a2a633cd4c9b607fe7 to your computer and use it in GitHub Desktop.
Save rherrmann/b1a2a633cd4c9b607fe7 to your computer and use it in GitHub Desktop.
SWT ScrolledComposite Examples to accompany this article: http://www.codeaffine.com/2016/03/01/swt-scrolledcomposite/
package com.codeaffine.scrolledcomposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* Brings face to face a ScrolledComposite with fixed content and, on the right side, a ScrolledComposite with expanding content.
*/
public class FixedVersusExpandingContent {
private static final int STYLE = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL;
public static void main( String[] args ) {
Display display = new Display();
Shell shell = new Shell( display );
shell.setText( "ScrolledComposite in 'scroll' and 'browser' mode" );
shell.setLayout( new FillLayout() );
ScrolledComposite leftSC = new ScrolledComposite( shell, STYLE );
CLabel leftLabel = new CLabel( leftSC, SWT.NONE );
leftLabel.setBackground( display.getSystemColor( SWT.COLOR_DARK_GREEN ) );
leftLabel.setForeground( display.getSystemColor( SWT.COLOR_WHITE ) );
String leftText
= "Fixed size content.\n\n"
+ "Scrollbars appear if the\n"
+ "ScrolledComposite\nis resized to be too small\n"
+ "to show the entire content.";
leftLabel.setText( leftText );
leftLabel.setAlignment( SWT.CENTER );
leftLabel.setSize( 250, 250 );
leftSC.setContent( leftLabel );
ScrolledComposite rightSC = new ScrolledComposite( shell, STYLE );
CLabel rightLabel = new CLabel( rightSC, SWT.NONE );
String rightText
= "Expanding content.\n\n"
+ "The content has a minimum size.\n"
+ "If the ScrolledComposite is\n"
+ "resized bigger than the minimum size,\n"
+ "the content will grow in size.\n"
+ "If the ScrolledComposite is made too small\n"
+ "to show the minimum size, scrollbars will appear.";
rightLabel.setText( rightText );
rightLabel.setBackground( display.getSystemColor( SWT.COLOR_DARK_GRAY ) );
rightLabel.setForeground( display.getSystemColor( SWT.COLOR_WHITE ) );
rightLabel.setAlignment( SWT.CENTER );
rightSC.setContent( rightLabel );
rightSC.setExpandHorizontal( true );
rightSC.setExpandVertical( true );
rightSC.setMinSize( 250, 250 );
shell.setSize( 600, 300 );
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
}
package com.codeaffine.scrolledcomposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* Shows an expanding ScrolledComposite that scrolls only vertically.
*/
public class VerticallyScrolledForm {
public static void main( String[] args ) {
new VerticallyScrolledForm().run();
}
private Shell shell;
private ScrolledComposite scrolledComposite;
void run() {
createControls();
layoutControls();
openShell();
runEventLoop();
}
private void createControls() {
shell = new Shell( new Display() );
shell.setText( "Contact Details Form" );
shell.addListener( SWT.Dispose, event -> event.display.dispose() );
scrolledComposite = new ScrolledComposite( shell, SWT.V_SCROLL );
Composite parent = createForm();
scrolledComposite.setContent( parent );
scrolledComposite.setExpandVertical( true );
scrolledComposite.setExpandHorizontal( true );
scrolledComposite.addListener( SWT.Resize, event -> {
int width = scrolledComposite.getClientArea().width;
scrolledComposite.setMinSize( parent.computeSize( width, SWT.DEFAULT ) );
} );
}
private void layoutControls() {
shell.setLayout( new GridLayout( 1, false ) );
GridData gridData = new GridData( SWT.FILL, SWT.FILL, true, true );
gridData.heightHint = computePreferredHeight();
scrolledComposite.setLayoutData( gridData );
}
private void openShell() {
shell.pack();
shell.open();
}
private void runEventLoop() {
while( !shell.isDisposed() ) {
if( !shell.getDisplay().readAndDispatch() ) {
shell.getDisplay().sleep();
}
}
}
private int computePreferredHeight() {
int numberOfLines = 10;
int defaultHorizontalSpacing = 5;
Text text = new Text( shell, SWT.BORDER );
text.setText( "X" );
Point preferredSize = text.computeSize( SWT.DEFAULT, SWT.DEFAULT );
text.dispose();
return numberOfLines * ( preferredSize.y + defaultHorizontalSpacing );
}
private Composite createForm() {
Composite result = new Composite( scrolledComposite, SWT.NONE );
result.setLayout( new GridLayout( 2, false ) );
createField( result, "Title" );
createField( result, "Salutation" );
createField( result, "First name" );
createField( result, "Middle name" );
createField( result, "Last name" );
createField( result, "Maiden name" );
createField( result, "Street" );
createField( result, "Street supplement" );
createField( result, "PO Box" );
createField( result, "Zip code" );
createField( result, "City" );
createField( result, "State" );
createField( result, "Country" );
createField( result, "Private Phone" );
createField( result, "Mobile Phone" );
createField( result, "Email address" );
createField( result, "Web site" );
return result;
}
private static void createField( Composite parent, String labelText ) {
Label label = new Label( parent, SWT.NONE );
label.setText( labelText );
Text text = new Text( parent, SWT.BORDER );
text.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment