Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem with GridLayout
Problem with GridLayout [message #448073] Thu, 30 December 2004 14:03 Go to next message
Stefan Pietsch is currently offline Stefan PietschFriend
Messages: 68
Registered: July 2009
Member
Hi,

I have some problem with GridLayout. I try to center a widget in a cell of
my GridLayout. But my code did not work.

Can somebody help?

Bye Stefan

import org.eclipse.swt.SWT;

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.Label;

import org.eclipse.swt.widgets.Shell;

public class CenterTest {

public static void main(String[] args) {

Display display = new Display();

Shell shell = new Shell(display);

shell.setLayout(new FillLayout());

shell.setText("CenterTest"); //$NON-NLS-1$

Composite composite = new Composite(shell, SWT.NONE);

composite.setLayout(new GridLayout(1, false));

composite.setBackground(display.getSystemColor(SWT.COLOR_BLU E));


Label label = new Label(composite, SWT.NONE);

label.setText("test"); //$NON-NLS-1$

label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER |
GridData.VERTICAL_ALIGN_CENTER));

label.setBackground(display.getSystemColor(SWT.COLOR_RED));


shell.open();

while (!shell.isDisposed()) {

if (!display.readAndDispatch()) {

display.sleep();

}

}

display.dispose();

}

}
Re: Problem with GridLayout [message #448077 is a reply to message #448073] Thu, 30 December 2004 16:15 Go to previous messageGo to next message
Stefan Zeiger is currently offline Stefan ZeigerFriend
Messages: 102
Registered: July 2009
Senior Member
Stefan Pietsch wrote:

> I have some problem with GridLayout. I try to center a widget in a cell of
> my GridLayout. But my code did not work.

You need to tell the Shell to lay out its children (recursively).

> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> shell.setText("CenterTest"); //$NON-NLS-1$
> Composite composite = new Composite(shell, SWT.NONE);
> composite.setLayout(new GridLayout(1, false));
> composite.setBackground(display.getSystemColor(SWT.COLOR_BLU E));
> Label label = new Label(composite, SWT.NONE);
> label.setText("test"); //$NON-NLS-1$
> label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER |
> GridData.VERTICAL_ALIGN_CENTER));
> label.setBackground(display.getSystemColor(SWT.COLOR_RED));

Add this line:
shell.pack();

> shell.open();

--
Stefan Zeiger http://www.szeiger.de http://www.novocode.com
My SWT controls: http://www.novocode.com/swt
Re: Problem with GridLayout [message #448108 is a reply to message #448077] Mon, 03 January 2005 08:06 Go to previous messageGo to next message
Stefan Pietsch is currently offline Stefan PietschFriend
Messages: 68
Registered: July 2009
Member
Hi Stefan,

I'm sorry, but your hint doesn't work.

The call of shell.pack() changed the size of my window. The label was
displayed in the center.
I tried shell.layout(), but this didn't help too.

In my opinion, shell.layout() is called in shell.open() before the window is
displayed.

Bye Stefan

"Stefan Zeiger" <szeiger@novocode.com> schrieb im Newsbeitrag
news:cr19i5$m1h$1@www.eclipse.org...
> Stefan Pietsch wrote:
>
> > I have some problem with GridLayout. I try to center a widget in a cell
of
> > my GridLayout. But my code did not work.
>
> You need to tell the Shell to lay out its children (recursively).
>
> > Display display = new Display();
> > Shell shell = new Shell(display);
> > shell.setLayout(new FillLayout());
> > shell.setText("CenterTest"); //$NON-NLS-1$
> > Composite composite = new Composite(shell, SWT.NONE);
> > composite.setLayout(new GridLayout(1, false));
> > composite.setBackground(display.getSystemColor(SWT.COLOR_BLU E));
> > Label label = new Label(composite, SWT.NONE);
> > label.setText("test"); //$NON-NLS-1$
> > label.setLayoutData(new
GridData(GridData.HORIZONTAL_ALIGN_CENTER |
> > GridData.VERTICAL_ALIGN_CENTER));
> > label.setBackground(display.getSystemColor(SWT.COLOR_RED));
>
> Add this line:
> shell.pack();
>
> > shell.open();
>
> --
> Stefan Zeiger http://www.szeiger.de http://www.novocode.com
> My SWT controls: http://www.novocode.com/swt
Re: Problem with GridLayout [message #448109 is a reply to message #448108] Mon, 03 January 2005 08:56 Go to previous messageGo to next message
Stefan Pietsch is currently offline Stefan PietschFriend
Messages: 68
Registered: July 2009
Member
Hi,

in correction to my last posting that the label wasn't centered in both
cases.

Bye Stefan

"Stefan Pietsch" <pietsch@multichart.de> schrieb im Newsbeitrag
news:crauf3$7lb$1@www.eclipse.org...
> Hi Stefan,
>
> I'm sorry, but your hint doesn't work.
>
> The call of shell.pack() changed the size of my window. The label was
> displayed in the center.
> I tried shell.layout(), but this didn't help too.
>
> In my opinion, shell.layout() is called in shell.open() before the window
is
> displayed.
>
> Bye Stefan
>
> "Stefan Zeiger" <szeiger@novocode.com> schrieb im Newsbeitrag
> news:cr19i5$m1h$1@www.eclipse.org...
> > Stefan Pietsch wrote:
> >
> > > I have some problem with GridLayout. I try to center a widget in a
cell
> of
> > > my GridLayout. But my code did not work.
> >
> > You need to tell the Shell to lay out its children (recursively).
> >
> > > Display display = new Display();
> > > Shell shell = new Shell(display);
> > > shell.setLayout(new FillLayout());
> > > shell.setText("CenterTest"); //$NON-NLS-1$
> > > Composite composite = new Composite(shell, SWT.NONE);
> > > composite.setLayout(new GridLayout(1, false));
> > >
composite.setBackground(display.getSystemColor(SWT.COLOR_BLU E));
> > > Label label = new Label(composite, SWT.NONE);
> > > label.setText("test"); //$NON-NLS-1$
> > > label.setLayoutData(new
> GridData(GridData.HORIZONTAL_ALIGN_CENTER |
> > > GridData.VERTICAL_ALIGN_CENTER));
> > > label.setBackground(display.getSystemColor(SWT.COLOR_RED));
> >
> > Add this line:
> > shell.pack();
> >
> > > shell.open();
> >
> > --
> > Stefan Zeiger http://www.szeiger.de http://www.novocode.com
> > My SWT controls: http://www.novocode.com/swt
>
>
Re: Problem with GridLayout [message #448119 is a reply to message #448108] Mon, 03 January 2005 17:09 Go to previous messageGo to next message
Stefan Zeiger is currently offline Stefan ZeigerFriend
Messages: 102
Registered: July 2009
Senior Member
Stefan Pietsch wrote:

> Hi Stefan,
>
> I'm sorry, but your hint doesn't work.
>
> The call of shell.pack() changed the size of my window. The label was
> displayed in the center.
> I tried shell.layout(), but this didn't help too.
>
> In my opinion, shell.layout() is called in shell.open() before the window is
> displayed.

You're right, of course. The layout is applied even without calling
pack(). The real problem is that you didn't set
grabExcessHorizontalSpace and grabExcessVerticalSpace on the GridData.
The following code should work:

GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER |
GridData.VERTICAL_ALIGN_CENTER);
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
label.setLayoutData(gd);

Setting only HORIZONTAL_ALIGN_CENTER centers the cell horizontally in
the space which is allocated for it in a parent Composite of minimum /
default size. GrabExcessHorizontalSpace keeps the cell centered even if
there's more than the minimum space available. Same for vertical.

--
Stefan Zeiger http://www.szeiger.de http://www.novocode.com
My SWT controls: http://www.novocode.com/swt
Re: Problem with GridLayout [message #448186 is a reply to message #448119] Wed, 05 January 2005 13:03 Go to previous message
Stefan Pietsch is currently offline Stefan PietschFriend
Messages: 68
Registered: July 2009
Member
Hi Stefan,

thanks. This solves it.

Bye Stefan

"Stefan Zeiger" <szeiger@novocode.com> schrieb im Newsbeitrag
news:crbu7k$h81$1@www.eclipse.org...
> Stefan Pietsch wrote:
>
> > Hi Stefan,
> >
> > I'm sorry, but your hint doesn't work.
> >
> > The call of shell.pack() changed the size of my window. The label was
> > displayed in the center.
> > I tried shell.layout(), but this didn't help too.
> >
> > In my opinion, shell.layout() is called in shell.open() before the
window is
> > displayed.
>
> You're right, of course. The layout is applied even without calling
> pack(). The real problem is that you didn't set
> grabExcessHorizontalSpace and grabExcessVerticalSpace on the GridData.
> The following code should work:
>
> GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER |
> GridData.VERTICAL_ALIGN_CENTER);
> gd.grabExcessHorizontalSpace = true;
> gd.grabExcessVerticalSpace = true;
> label.setLayoutData(gd);
>
> Setting only HORIZONTAL_ALIGN_CENTER centers the cell horizontally in
> the space which is allocated for it in a parent Composite of minimum /
> default size. GrabExcessHorizontalSpace keeps the cell centered even if
> there's more than the minimum space available. Same for vertical.
>
> --
> Stefan Zeiger http://www.szeiger.de http://www.novocode.com
> My SWT controls: http://www.novocode.com/swt
Previous Topic:One more label in Section.
Next Topic:swt.dll fro windows ce and mips?
Goto Forum:
  


Current Time: Tue Apr 16 08:30:35 GMT 2024

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

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

Back to the top