Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Layout size adjusted to its controls prefered size
Layout size adjusted to its controls prefered size [message #515925] Mon, 22 February 2010 09:23 Go to next message
El Mariachi is currently offline El MariachiFriend
Messages: 11
Registered: February 2010
Junior Member
Hi all,

I'm wondering if there is a layout which size is automatically adjusted to the prefered size of it's children.

In details, what I need to do is a new composite on which I add a new StyledText widget. I want the StyledText to automatically adapt it's size to it's text content. (that's to say always adjusting to it's prefered size). The FillLayout doesn't fill my needs because the StyledText is always resized to the parent composite's size.

So does such a layout exists, or do I need to make my own?

Thanks...



Emiliano El Mariachi
Re: Layout size adjusted to its controls prefered size [message #515932 is a reply to message #515925] Mon, 22 February 2010 09:43 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
all other layouts apart from FillLayout does that only i suppose....

Try GridLayout on youy composite and Griddata on your StyledText..

Note: But in case of Text and StyledText controls instead of having a prefered size, the size should be fixed and scrollbars should be there to show the content.


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay

[Updated on: Mon, 22 February 2010 09:45]

Report message to a moderator

Re: Layout size adjusted to its controls prefered size [message #515964 is a reply to message #515932] Mon, 22 February 2010 11:26 Go to previous messageGo to next message
El Mariachi is currently offline El MariachiFriend
Messages: 11
Registered: February 2010
Junior Member
First of all, thanks for your replay... But I still don't manage to do what I need to do...

Here is a piece of code :

package org.eclipse.swt.snippets;


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
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;

public class TestLayoutStyledText {

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);

shell.setLayout(new FillLayout());

//ScrolledComposite placed on the Shell with a FillLayout
ScrolledComposite oScrolledComposite = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
oScrolledComposite.setLayout(new FillLayout());
oScrolledComposite.setExpandHorizontal(true);
oScrolledComposite.setExpandVertical(true);

//Composite containing StyledText controls and placed on the ScrolledComposite
Composite oControlsComposite = new Composite(oScrolledComposite, SWT.NONE);
oControlsComposite.setBackground(new Color(null,0,255,255));

//Affects the ContolsComposite with a GridLayout of 1 column
GridLayout oGridLayout = new GridLayout();
oGridLayout.numColumns = 1;
oControlsComposite.setLayout(oGridLayout);

//StyledText 1 placed in the ControlsComposite with a GridLayout
StyledText oStyledText1 = new StyledText(oControlsComposite,SWT.NONE);
GridData oGridData1 = new GridData(GridData.FILL_BOTH);
oGridData1.grabExcessHorizontalSpace = true;
oGridData1.grabExcessVerticalSpace = true;
oGridData1.minimumHeight = 10;
oGridData1.minimumWidth = 10;
oStyledText1.setLayoutData(oGridData1);

//StyledText 2 placed under StyledText 1 in the ControlsComposite with a GridLayout
StyledText oStyledText2 = new StyledText(oControlsComposite,SWT.NONE);
GridData oGridData2 = new GridData(GridData.FILL_BOTH);
oGridData2.grabExcessHorizontalSpace = true;
oGridData2.grabExcessVerticalSpace = true;
oGridData2.minimumHeight = 10;
oGridData2.minimumWidth = 10;
oStyledText2.setLayoutData(oGridData2);

//Affects the content of the ScrolledComposite with the composite containing Controls
oScrolledComposite.setContent(oControlsComposite);

shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}


The behavior I expect is that when the first StyledText is filled up with text which is greater than the StyledText's size, I want to StyledText to grow so that all the text can still be visible. Then I want the second StyledText to move down.
I'm trying to use the ScrolledComposite so that if the size of each StyledText is greater then the shell size, then ScrollBars would appear.



Hope my example is more explicit.



Emiliano El Mariachi
Re: Layout size adjusted to its controls prefered size [message #515987 is a reply to message #515964] Mon, 22 February 2010 12:05 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Why can not the same thing done like this...

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
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;

public class TestLayoutStyledText
{

    public static void main(String[] args)
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        // ScrolledComposite placed on the Shell with a FillLayout
        ScrolledComposite oScrolledComposite = new ScrolledComposite(shell, SWT.BORDER
                | SWT.H_SCROLL | SWT.V_SCROLL);
        oScrolledComposite.setLayout(new FillLayout());
        oScrolledComposite.setExpandHorizontal(true);
        oScrolledComposite.setExpandVertical(true);

        // Composite containing StyledText controls and placed on the ScrolledComposite
        Composite oControlsComposite = new Composite(oScrolledComposite, SWT.NONE);
        oControlsComposite.setBackground(new Color(null, 0, 255, 255));

        // Affects the ContolsComposite with a GridLayout of 1 column
        GridLayout oGridLayout = new GridLayout();
        oGridLayout.numColumns = 1;
        oControlsComposite.setLayout(oGridLayout);

        // StyledText 1 placed in the ControlsComposite with a GridLayout
        StyledText oStyledText1 = new StyledText(oControlsComposite, SWT.H_SCROLL | SWT.V_SCROLL);
        GridData oGridData1 = new GridData(GridData.FILL_BOTH);
        oGridData1.grabExcessHorizontalSpace = true;
        oGridData1.grabExcessVerticalSpace = true;
        oGridData1.minimumHeight = 10;
        oGridData1.minimumWidth = 10;
        oStyledText1.setLayoutData(oGridData1);

        // StyledText 2 placed under StyledText 1 in the ControlsComposite with a GridLayout
        StyledText oStyledText2 = new StyledText(oControlsComposite, SWT.H_SCROLL | SWT.V_SCROLL);
        GridData oGridData2 = new GridData(GridData.FILL_BOTH);
        oGridData2.grabExcessHorizontalSpace = true;
        oGridData2.grabExcessVerticalSpace = true;
        oGridData2.minimumHeight = 10;
        oGridData2.minimumWidth = 10;
        oStyledText2.setLayoutData(oGridData2);

        // Affects the content of the ScrolledComposite with the composite containing Controls
        oScrolledComposite.setContent(oControlsComposite);

        shell.setSize(400, 300);
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}


or do you want exactly what you specified....


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay

[Updated on: Mon, 22 February 2010 12:06]

Report message to a moderator

Re: Layout size adjusted to its controls prefered size [message #515992 is a reply to message #515987] Mon, 22 February 2010 12:25 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
You owe me one...

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
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;

public class TestLayoutStyledText
{

    private static GridData oGridData2;

    private static ScrolledComposite oScrolledComposite;

    private static Composite oControlsComposite;

    private static StyledText oStyledText1;

    private static GridData oGridData1;

    public static void main(String[] args)
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        // ScrolledComposite placed on the Shell with a FillLayout
        oScrolledComposite = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        oScrolledComposite.setLayout(new FillLayout());
        oScrolledComposite.setExpandHorizontal(true);
        oScrolledComposite.setExpandVertical(true);

        // Composite containing StyledText controls and placed on the ScrolledComposite
        oControlsComposite = new Composite(oScrolledComposite, SWT.NONE);
        oControlsComposite.setBackground(new Color(null, 0, 255, 255));

        // Affects the ContolsComposite with a GridLayout of 1 column
        GridLayout oGridLayout = new GridLayout();
        oGridLayout.numColumns = 1;
        oControlsComposite.setLayout(oGridLayout);

        // StyledText 1 placed in the ControlsComposite with a GridLayout
        oStyledText1 = new StyledText(oControlsComposite, SWT.None);
        oGridData1 = new GridData(GridData.FILL_BOTH);
        oGridData1.grabExcessHorizontalSpace = true;
        oGridData1.grabExcessVerticalSpace = true;
        oGridData1.minimumHeight = 10;
        oGridData1.minimumWidth = 10;
        oStyledText1.setLayoutData(oGridData1);
        oStyledText1.addModifyListener(new ModifyListener()
        {

            @Override
            public void modifyText(ModifyEvent e)
            {
                Point computeSize = oStyledText1.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
                oGridData1.minimumHeight = computeSize.y;
                oGridData1.minimumWidth = computeSize.x;
                oControlsComposite.layout();
                oScrolledComposite.setMinSize(oControlsComposite.computeSize(SWT.DEFAULT,
                        SWT.DEFAULT));
            }
        });

        // StyledText 2 placed under StyledText 1 in the ControlsComposite with a GridLayout
        final StyledText oStyledText2 = new StyledText(oControlsComposite, SWT.None);
        oGridData2 = new GridData(GridData.FILL_BOTH);
        oGridData2.grabExcessHorizontalSpace = true;
        oGridData2.grabExcessVerticalSpace = true;
        oGridData2.minimumHeight = 10;
        oGridData2.minimumWidth = 10;
        oStyledText2.addModifyListener(new ModifyListener()
        {

            @Override
            public void modifyText(ModifyEvent e)
            {
                Point computeSize = oStyledText2.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
                oGridData2.minimumHeight = computeSize.y;
                oGridData2.minimumWidth = computeSize.x;
                oControlsComposite.layout();
                oScrolledComposite.setMinSize(oControlsComposite.computeSize(SWT.DEFAULT,
                        SWT.DEFAULT));
            }
        });
        oStyledText2.setLayoutData(oGridData2);

        // Affects the content of the ScrolledComposite with the composite containing Controls
        oScrolledComposite.setContent(oControlsComposite);

        shell.setSize(400, 300);
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Layout size adjusted to its controls prefered size [message #516043 is a reply to message #515992] Mon, 22 February 2010 15:11 Go to previous messageGo to next message
El Mariachi is currently offline El MariachiFriend
Messages: 11
Registered: February 2010
Junior Member
Thanks a lot, you're right I owe you one!!!

Your solution is working fine... You're doing a trick with the text change listener, and adjusting the StyledText size on Text change, but I thought choosing a good Layout and/or good DataLayout would do it automatically. Why the DataLayout doesn't call automatically the computeSize method on StyledText Text change? Maybe the StyledText doen't perform any query of size change when its text is modified.

Anyway, thanks a lot for your replies!



Emiliano El Mariachi
Re: Layout size adjusted to its controls prefered size [message #525599 is a reply to message #515992] Wed, 07 April 2010 01:30 Go to previous message
John is currently offline JohnFriend
Messages: 4
Registered: July 2009
Junior Member
Hi Vijay,

Could you please fixed the width of StyledText, only adjust the height?
Do you have a good idea?

Best regards,
John

On 02/22/2010 08:25 PM, vijay wrote:
> You owe me one...
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.custom.ScrolledComposite;
> import org.eclipse.swt.custom.StyledText;
> import org.eclipse.swt.events.ModifyEvent;
> import org.eclipse.swt.events.ModifyListener;
> import org.eclipse.swt.graphics.Color;
> import org.eclipse.swt.graphics.Point;
> import org.eclipse.swt.layout.FillLayout;
> 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;
>
> public class TestLayoutStyledText
> {
>
> private static GridData oGridData2;
>
> private static ScrolledComposite oScrolledComposite;
>
> private static Composite oControlsComposite;
>
> private static StyledText oStyledText1;
>
> private static GridData oGridData1;
>
> public static void main(String[] args)
> {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
>
> // ScrolledComposite placed on the Shell with a FillLayout
> oScrolledComposite = new ScrolledComposite(shell, SWT.BORDER |
> SWT.H_SCROLL | SWT.V_SCROLL);
> oScrolledComposite.setLayout(new FillLayout());
> oScrolledComposite.setExpandHorizontal(true);
> oScrolledComposite.setExpandVertical(true);
>
> // Composite containing StyledText controls and placed on the
> ScrolledComposite
> oControlsComposite = new Composite(oScrolledComposite, SWT.NONE);
> oControlsComposite.setBackground(new Color(null, 0, 255, 255));
>
> // Affects the ContolsComposite with a GridLayout of 1 column
> GridLayout oGridLayout = new GridLayout();
> oGridLayout.numColumns = 1;
> oControlsComposite.setLayout(oGridLayout);
>
> // StyledText 1 placed in the ControlsComposite with a GridLayout
> oStyledText1 = new StyledText(oControlsComposite, SWT.None);
> oGridData1 = new GridData(GridData.FILL_BOTH);
> oGridData1.grabExcessHorizontalSpace = true;
> oGridData1.grabExcessVerticalSpace = true;
> oGridData1.minimumHeight = 10;
> oGridData1.minimumWidth = 10;
> oStyledText1.setLayoutData(oGridData1);
> oStyledText1.addModifyListener(new ModifyListener()
> {
>
> @Override
> public void modifyText(ModifyEvent e)
> {
> Point computeSize = oStyledText1.computeSize(SWT.DEFAULT,
> SWT.DEFAULT, true);
> oGridData1.minimumHeight = computeSize.y;
> oGridData1.minimumWidth = computeSize.x;
> oControlsComposite.layout();
>
> oScrolledComposite.setMinSize(oControlsComposite.computeSize (SWT.DEFAULT,
> SWT.DEFAULT));
> }
> });
>
> // StyledText 2 placed under StyledText 1 in the
> ControlsComposite with a GridLayout
> final StyledText oStyledText2 = new
> StyledText(oControlsComposite, SWT.None);
> oGridData2 = new GridData(GridData.FILL_BOTH);
> oGridData2.grabExcessHorizontalSpace = true;
> oGridData2.grabExcessVerticalSpace = true;
> oGridData2.minimumHeight = 10;
> oGridData2.minimumWidth = 10;
> oStyledText2.addModifyListener(new ModifyListener()
> {
>
> @Override
> public void modifyText(ModifyEvent e)
> {
> Point computeSize = oStyledText2.computeSize(SWT.DEFAULT,
> SWT.DEFAULT, true);
> oGridData2.minimumHeight = computeSize.y;
> oGridData2.minimumWidth = computeSize.x;
> oControlsComposite.layout();
>
> oScrolledComposite.setMinSize(oControlsComposite.computeSize (SWT.DEFAULT,
> SWT.DEFAULT));
> }
> });
> oStyledText2.setLayoutData(oGridData2);
>
> // Affects the content of the ScrolledComposite with the
> composite containing Controls
> oScrolledComposite.setContent(oControlsComposite);
>
> shell.setSize(400, 300);
> shell.open();
> while (!shell.isDisposed())
> {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
> }
> }
>
Previous Topic:SWT applet. I don't see anything. Please help!
Next Topic:Checkbox on table header
Goto Forum:
  


Current Time: Sat Dec 07 15:09:49 GMT 2024

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

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

Back to the top