Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Need a Workaround to get a Working ScrollBar
Need a Workaround to get a Working ScrollBar [message #467064] Mon, 23 January 2006 21:25 Go to next message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
R3.1.1

Since scrolling is broken in ScrolledForm (see
https://bugs.eclipse.org/bugs/show_bug.cgi?id=103420), I need to "roll my own" vertical scroll-bar
for a simple Composite that contains 0..N child Composites (arranged vertically in a one-column
GridLayout). I've tried using ScrolledComposite, but I seem to be missing something, since I have
not succeeded in getting anything useful. Any help would be much appreciated!

I've tried using the ScrolledComposite, according to "method 1" OR "method 2" of its javadocs; both
give a tiny, almost invisible, Composite (with a 4-pixel-high scroll-bar):

_formToolkit = new FormToolkit(parent.getDisplay());
_theForm = _formToolkit.createForm(parent);
_formToolkit.paintBordersFor(_theForm.getBody());

final ScrolledComposite sc = new ScrolledComposite(_theForm.getBody(), SWT.V_SCROLL);
/* adding this line does not help:
toolkit().adapt(sc);
*/

//// THIS IS THE COMPOSITE THAT NEEDS THE SCROLL-BARS:
_topLevel = _formToolkit.createComposite(sc, SWT.V_SCROLL); // ALSO tried with SWT.NONE

/* add the next two lines for "method 2"
sc.setExpandVertical(true);
sc.setMinHeight(50);
*/
sc.setContent(_topLevel);
_topLevel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
_topLevel.setLayout(new GridLayout());

// .. after each time the contents of the Composite change:
_topLevel.setSize(_topLevel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
/* change the above line to the following for "method 2":
sc.setMinSize(_topLevel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
*/
_topLevel.layout();

Thanks,
Paul
Re: Need a Workaround to get a Working ScrollBar [message #467080 is a reply to message #467064] Tue, 24 January 2006 15:31 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
See my comments below:

"Paul Keyser" <rolarenfan@earthlink.net> wrote in message
news:dr3hka$quo$1@utils.eclipse.org...
>
> _formToolkit = new FormToolkit(parent.getDisplay());
> _theForm = _formToolkit.createForm(parent);
> _formToolkit.paintBordersFor(_theForm.getBody());

--------------------- Have you assigned a layout to _theForm? If it is a
GridLayout you should assign some GridData to sc like new
GridData(SWT.FILLL, SWT.FILL, true, true)
--------------------- Also, what is the layout of parent? You may need to
assign some layout data to _theForm

>
> final ScrolledComposite sc = new ScrolledComposite(_theForm.getBody(),
> SWT.V_SCROLL);
> /* adding this line does not help:
> toolkit().adapt(sc);
> */
>
> //// THIS IS THE COMPOSITE THAT NEEDS THE SCROLL-BARS:

--------------------- _topLevel should not be created with SWT.V_SCROLL -
this will just give you two sets of scrollbars

> _topLevel = _formToolkit.createComposite(sc, SWT.V_SCROLL); // ALSO
> tried with SWT.NONE
>
> /* add the next two lines for "method 2"
> sc.setExpandVertical(true);
> sc.setMinHeight(50);
> */
> sc.setContent(_topLevel);

--------------------- sc does not have a GridLayout so setting GridData on
_topLevel makes no sense, ScrolledComposite has its own internal layout, not
layout data for content is required

> _topLevel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
> _topLevel.setLayout(new GridLayout());
>
> // .. after each time the contents of the Composite change:
> _topLevel.setSize(_topLevel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
> /* change the above line to the following for "method 2":
> sc.setMinSize(_topLevel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
> */

--------------------- Calling _topLevel.setSize() already causes a layout
so this additional layout call is unneccessary

> _topLevel.layout();
>
> Thanks,
> Paul
Re: Need a Workaround to get a Working ScrollBar [message #467092 is a reply to message #467080] Tue, 24 January 2006 17:12 Go to previous messageGo to next message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
Veronika --

Thanks, but it still fails:

a) Layout on "Form": the javadocs for this class are inconsistent, and the example code does not
suggest setting a layout; I have twice succeeded in getting good behavior with no layout, and when I
added a FillLayout to those two cases, it made no difference, whereas adding a FillLayout to my
problem case does not help.

Here are the inconsistent javadocs for Form:
<code>Form has a custom layout manager that is wrap-enabled. If a form is placed in a composite
whose layout manager implements ILayoutExtension, the body of the worm will participate in wrapping
as long as its layout manager implements ILayoutExtension as well.

Children of the form should typically be created using FormToolkit to match the appearance and
behaviour. When creating children, use the form body as a parent by calling 'getBody()' on the form
instance. Example:

FormToolkit toolkit = new FormToolkit(parent.getDisplay());
Form form = toolkit.createForm(parent);
formContent.setText("Sample form");
formContent.getBody().setLayout(new GridLayout());
toolkit.createButton(formContent.getBody(), "Checkbox", SWT.CHECK);

No layout manager has been set on the body. Clients are required to set the desired layout manager
explicitly. </code>


b) The "parent" Composite is the one supplied by the Eclipse framework when I create my ViewPart; I
usually do not need to set Layout on that.


c) I do set a GridLayout on _theForm.getBody(), although I omitted to send that part of the code
(sorry); other widgets, added above (before) the ScrolledComposite, behave reasonably (a Group with
three radio-buttons). Here's the code, now:

// "parent" supplied to method createPartControl() of IViewPart
_formToolkit = new FormToolkit(parent.getDisplay());
_theForm = _formToolkit.createForm(parent);
_theForm.setLayout(new FillLayout());

_formToolkit.paintBordersFor(_theForm.getBody());

final GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
layout.verticalSpacing = 0;
_theForm.getBody().setLayout(layout);

// THIS GROUP BEHAVES CORRECTLY
final Group group = new Group(_theForm.getBody(), SWT.NONE);
_formToolkit.adapt(group);
group.setText(Messages.getString("HitListKeys.ShowHits")); //$NON-NLS-1$
group.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

final GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
layout.verticalSpacing = 0;
group.setLayout(layout);

String label = Messages.getString("HitListKeys.Passage"); //$NON-NLS-1$
_radioPassage = _formToolkit).createButton(group, label, SWT.RADIO);
_radioPassage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
_radioPassage.setEnabled(false);

label = Messages.getString("HitListKeys.Document"); //$NON-NLS-1$
_radioDocument = _formToolkit.createButton(group, label, SWT.RADIO);
_radioDocument.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
_radioDocument.setSelection(true);
_radioDocument.setEnabled(false);

label = Messages.getString("HitListKeys.Conventional"); //$NON-NLS-1$
_radioOldstyle = _formToolkit.createButton(group, label, SWT.RADIO);
_radioOldstyle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
_radioOldstyle.setEnabled(false);

// IF I HERE CREATE A TABLE-VIEWER, instead of a ScrolledComposite, IT BEHAVES CORRECTLY
final ScrolledComposite sc = new ScrolledComposite(_theForm.getBody(), SWT.V_SCROLL);

_topLevel = _formToolkit.createComposite(sc, SWT.NONE);
sc.setContent(_topLevel);
_topLevel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
_topLevel.setLayout(new GridLayout());

// .. after each time the contents of the Composite _topLevel change:
_topLevel.setSize(_topLevel.computeSize(SWT.DEFAULT, SWT.DEFAULT));



d) in my first implementation of the GUI, I used a TableViewer instead of a Composite having 0..N
children, and the TableViewer always behaves correctly.

So, I am still puzzled/stuck ...

Paul
Re: Need a Workaround to get a Working ScrollBar [message #467336 is a reply to message #467092] Sun, 29 January 2006 09:47 Go to previous messageGo to next message
Sanjay Chaudhuri is currently offline Sanjay ChaudhuriFriend
Messages: 19
Registered: July 2009
Junior Member
A code snippet to have ScrolledComposite with GridLayout and FormToolkit. This can be easily modfied, to be used in Form.

<code>
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.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

public class FormToolkitExample
{
 public static void main(String[] args)
 {
   Display display = new Display();
   Shell shell = new Shell( display );
   shell.setLayout( new GridLayout( 1, false));

   ScrolledComposite scrlComposite = new ScrolledComposite( shell, SWT.H_SCROLL | SWT.V_SCROLL);
   GridLayout gridScrlComposite = new GridLayout( 1, false);
   scrlComposite.setLayout( gridScrlComposite);
   scrlComposite.setLayoutData( new GridData( GridData.FILL_BOTH));

   Composite composite = new Composite( scrlComposite, SWT.NONE);
   scrlComposite.setContent( composite);
   GridLayout gridComposite = new GridLayout( 1, false);
   composite.setLayout( gridComposite);
   composite.setLayoutData( new GridData( GridData.FILL_BOTH));

   FormToolkit formToolkit = new FormToolkit( display);
   formToolkit.paintBordersFor( composite);

   Text txtField = formToolkit.createText( composite, "", SWT.NONE );

   Point p = composite.computeSize( SWT.DEFAULT, SWT.DEFAULT, true );
   scrlComposite.setMinWidth( p.x );
   scrlComposite.setMinHeight( p.y );

   scrlComposite.setExpandHorizontal( true );
   scrlComposite.setExpandVertical( true );

   shell.pack();
   shell.open();

   while ( !shell.isDisposed() )
   {
     if ( !display.readAndDispatch() )
       display.sleep();
   }
   display.dispose();
 }
}
</code>
</code>
Re: Need a Workaround to get a Working ScrollBar [message #467367 is a reply to message #467336] Mon, 30 January 2006 16:18 Go to previous messageGo to next message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
Sanjay --

Many thanks, having working code was just what I needed to see where I had gone wrong: I had no
Layout-data on my ScrolledComposite! Now all is well, with thanks to you. (I am still amazed at how
very hard it is to get simple layout/scrolling correct in SWT, as compared -- say -- to Swing.)

(I also needed to set the background colors on various things, since they are not created through
the FormToolkit.)

Paul
Re: Need a Workaround to get a Working ScrollBar [message #467400 is a reply to message #467336] Mon, 30 January 2006 21:42 Go to previous messageGo to next message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
Here's another thing that is quite easy in Swing, and (to me) incomprehensible in SWT: if I create
the ScrolledComposite with just SWT.V_SCROLL, instead of doing the sensible thing and constraining
its notion of its size to that of its containing widget, it imagines that it is *infinitely* wide,
so any Text widget that I put into the ScrolledComposite extends indefinitely to the right (rather
than, as would be sensible, wrapping. So, even though I don't want it, I have to have horiz. scrolling!

(I'm actually using FormText, because I need the HTML-rendering, and it has SWT.WRAP, which is
evidently being ignored; if I use Text or Label, with an explicit SWT.WRAP, the ScrolledComposite
also treats it as of infinite width. I've tried stuffing in a variety of internmediate widgets, and
playing around with a variety of GridData objects: no luck.)

Any clues on that one?

thanks,
Paul
Re: Need a Workaround to get a Working ScrollBar [message #467469 is a reply to message #467400] Tue, 31 January 2006 18:22 Go to previous messageGo to next message
Sanjay Chaudhuri is currently offline Sanjay ChaudhuriFriend
Messages: 19
Registered: July 2009
Junior Member
Can you please get me a small snap-shot of a working piece of proto-code, like the one I had posted, so that I can look into it, and try to give you a solution.

It would be best, if you can modify my code, and put, whatever you would like me to look into. I would be more than happy to help you.

By the way, to address one of the other issues you had mentioned about setting the backgrounds manually for each and every control, I would advise you to create a helper object for those tasks. Infact what I did is, I created an intelligent object, to take care of all those necessary but repeated tasks for each and every control, like setting the composite, backgrounds, GridData (which I modified to be more like HTML code, so that the front-end GUI client code is very very clean), and to take care of lot of things automatically, rather than you trying to put it. Basically in a nutshell, this object does all, whatever an end-user programmer needs to fill in to get a GUI screen up properly.

Thanks

Sanjay
Re: Need a Workaround to get a Working ScrollBar [message #467496 is a reply to message #467469] Wed, 01 February 2006 03:40 Go to previous message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
Sanjay --

Thanks for the offer, but doing that will take some time -- meanwhile, I have entered a bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=125969 whose text and attachment should help clarify
the strange problem.

Don't you find SWT layouts MUCH harder than Swing? (After two months of learning Swing I could
reliably do better layouts thanI can after two years of learning SWT.)

thanks,
Paul
Previous Topic:swt_awt bridge
Next Topic:JFace Data Binding - nested properties
Goto Forum:
  


Current Time: Thu Apr 18 07:37:59 GMT 2024

Powered by FUDForum. Page generated in 0.02506 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top