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 04:23  |
Eclipse User |
|
|
|
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...
|
|
| |
Re: Layout size adjusted to its controls prefered size [message #515964 is a reply to message #515932] |
Mon, 22 February 2010 06:26   |
Eclipse User |
|
|
|
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.
|
|
|
Re: Layout size adjusted to its controls prefered size [message #515987 is a reply to message #515964] |
Mon, 22 February 2010 07:05   |
Eclipse User |
|
|
|
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....
[Updated on: Mon, 22 February 2010 07:06] by Moderator
|
|
|
Re: Layout size adjusted to its controls prefered size [message #515992 is a reply to message #515987] |
Mon, 22 February 2010 07:25   |
Eclipse User |
|
|
|
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();
}
}
|
|
| |
Re: Layout size adjusted to its controls prefered size [message #525599 is a reply to message #515992] |
Tue, 06 April 2010 21:30  |
Eclipse User |
|
|
|
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();
> }
> }
>
|
|
|
Goto Forum:
Current Time: Wed Jul 23 08:31:01 EDT 2025
Powered by FUDForum. Page generated in 0.33790 seconds
|