Created
March 6, 2017 21:07
-
-
Save stefanhaustein/7d70ec9e8800e9c4be1cb3e35fe33787 to your computer and use it in GitHub Desktop.
Fork of https://gist.github.com/rherrmann/b1a2a633cd4c9b607fe7 without resize listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original: https://gist.github.com/rherrmann/b1a2a633cd4c9b607fe7 | |
// 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 ); | |
} | |
private void layoutControls() { | |
shell.setLayout( new GridLayout( 1, false ) ); | |
int contentHeight = scrolledComposite.getContent().computeSize(SWT.DEFAULT, SWT.DEFAULT).y; | |
scrolledComposite.setMinHeight( contentHeight ); | |
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; | |
Composite parent = (Composite) scrolledComposite.getContent(); | |
int contentHeight = parent.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; | |
return contentHeight * numberOfLines / (parent.getChildren().length / 2); | |
} | |
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