Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » setLocation Problem when resizing shell
setLocation Problem when resizing shell [message #450610] Wed, 16 February 2005 09:36 Go to next message
Peter Goetz is currently offline Peter GoetzFriend
Messages: 38
Registered: July 2009
Member
Hi out there,

I have a little question for those of you who are deeper inside the code
of SWT.
I have a shell with a composite that is being placed via setLocation
when the shell resizes. Unfortunately the setLocation seems to be
uneffective and I reckon, after I set the Location there is someone else
who does the same. I think it might be the shell when it lays out it's
composites after the resize.
The composite is not part of a layout so I don't really want to have it
laid out automatically (the location is 0/0 after resize).
Is there a possibility not to set the Location of my composite on the
shell automatically or is there some other mistake I'm making?
Thanks a lot and have a nice day,

Peter
Re: setLocation Problem when resizing shell [message #450646 is a reply to message #450610] Wed, 16 February 2005 14:11 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
You're going to have to code a small stand alone snippet that shows then
problem. I can't understand it from your description.

"Peter Goetz" <kingofbrain@web.de> wrote in message
news:cuv49q$vn5$1@www.eclipse.org...
> Hi out there,
>
> I have a little question for those of you who are deeper inside the code
> of SWT.
> I have a shell with a composite that is being placed via setLocation
> when the shell resizes. Unfortunately the setLocation seems to be
> uneffective and I reckon, after I set the Location there is someone else
> who does the same. I think it might be the shell when it lays out it's
> composites after the resize.
> The composite is not part of a layout so I don't really want to have it
> laid out automatically (the location is 0/0 after resize).
> Is there a possibility not to set the Location of my composite on the
> shell automatically or is there some other mistake I'm making?
> Thanks a lot and have a nice day,
>
> Peter
Re: setLocation Problem when resizing shell [message #450651 is a reply to message #450646] Wed, 16 February 2005 15:29 Go to previous messageGo to next message
Peter Goetz is currently offline Peter GoetzFriend
Messages: 38
Registered: July 2009
Member
Steve Northover wrote:
> You're going to have to code a small stand alone snippet that shows then
> problem. I can't understand it from your description.
>
> "Peter Goetz" <kingofbrain@web.de> wrote in message
> [...]

Hi Steve,

thank you very much for your answer. I have prepared a relatively small
piece of code that illustrates the error.
It is attached below.
The important thing happens in line 115 where I call setLocation on the
Composite.
If you execute the program and open or close the "fastview", everything
is fine. But if you resize the shell, the Composite gets the location 0/0.
I hope you can help me!
Thanks a lot in advance.

Peter

===== CODE =====

package de.kaobe.fastview;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
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;

/**
* @author pgoetz
*/
public class ErrorSnippet
{
public static void main(String[] args)
{
ErrorSnippet snippet = new ErrorSnippet();
}

public ErrorSnippet()
{
Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
FastView fastview = new FastView(shell, SWT.NONE, "address",
new TestComposite(shell, SWT.NONE));
FormData formData;

formData = new FormData(SWT.DEFAULT,
fastview.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
formData.top = new FormAttachment(75);
formData.left = new FormAttachment(25);
formData.right = new FormAttachment(75);

fastview.setLayoutData(formData);

shell.setLayout(new FormLayout());
shell.open();

while(!shell.isDisposed())
{
while(!display.readAndDispatch())
{
display.sleep();
}
}

display.dispose();
}

class FastView extends Composite
{
private Label title;
private Button button;
private Composite content;
private boolean isExtended = false;

public FastView(Composite parent, int style, String title,
Composite content)
{
super(parent, style);

this.content = content;

initWidgets(title);
layoutWidgets();
pack();
}

private void changeFastViewState()
{
if(isExtended)
{
button.setText("open");
}
else
{
button.setText("close");
}

isExtended = !isExtended;

content.setVisible(isExtended);
content.setFocus();
}

private void initWidgets(String titleText)
{
title = new Label(this, SWT.NONE);
button = new Button(this, SWT.FLAT);

content.setVisible(false);

setBackground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
addControlListener(new ControlAdapter()
{
public void controlResized(ControlEvent e)
{
layoutContents();
}

private void layoutContents()
{
int x = getBounds().x;
int y = getBounds().y - content.getBounds().height;
int width = getBounds().width;
int height = content.getBounds().height;
Rectangle bounds = new Rectangle(x, y, width, height);

content.setBounds(bounds);
}
});

title.setText(titleText);

button.setText("open");
button.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
changeFastViewState();
}
});
}

private void layoutWidgets()
{
FormLayout layout = new FormLayout();
FormData formData;

setLayout(layout);

formData = new FormData();
formData.top = new FormAttachment(button, 0, SWT.CENTER);
formData.left = new FormAttachment(5);
formData.right = new FormAttachment(button);
title.setLayoutData(formData);

formData = new FormData();
formData.top = new FormAttachment(0);
formData.left = new FormAttachment(50,
-button.computeSize(SWT.DEFAULT, SWT.DEFAULT).x / 2);
button.setLayoutData(formData);
}
}

class TestComposite extends Composite
{
private Label lblFirstName;
private Label lblLastName;
private Text txtFirstName;
private Text txtLastName;

public TestComposite(Composite parent, int style)
{
super(parent, style);

setBackground(getDisplay().getSystemColor(SWT.COLOR_WHITE));

initWidgets();
layoutWidgets();
fillWidgets("Peter", "Goetz");
pack();
}

private void initWidgets()
{
lblFirstName = new Label(this, SWT.NONE);
lblLastName = new Label(this, SWT.NONE);
txtFirstName = new Text(this, SWT.BORDER);
txtLastName = new Text(this, SWT.BORDER);

lblFirstName.setText("firstname:");
lblLastName.setText("lastname:");
}

private void layoutWidgets()
{
FormData formData;
FormLayout layout = new FormLayout();
int broadMargin = 5;

layout.marginWidth = layout.marginHeight = 10;

this.setLayout(layout);

// first row
formData = new FormData(100, SWT.DEFAULT);
formData.top = new FormAttachment(0);
formData.left = new FormAttachment(0);
formData.right = new FormAttachment(50);
lblFirstName.setLayoutData(formData);

formData = new FormData(100, SWT.DEFAULT);
formData.top = new FormAttachment(lblFirstName, 0, SWT.CENTER);
formData.left = new FormAttachment(lblFirstName);
formData.right = new FormAttachment(100);
lblLastName.setLayoutData(formData);

formData = new FormData();
formData.top = new FormAttachment(lblFirstName);
formData.left = new FormAttachment(lblFirstName, 0, SWT.LEFT);
formData.right = new FormAttachment(lblFirstName, 0,
SWT.RIGHT);
txtFirstName.setLayoutData(formData);

formData = new FormData();
formData.top = new FormAttachment(lblLastName);
formData.left = new FormAttachment(lblLastName, 0, SWT.LEFT);
formData.right = new FormAttachment(lblLastName, 0, SWT.RIGHT);
txtLastName.setLayoutData(formData);
}

private void fillWidgets(String firstname, String lastname)
{
txtFirstName.setText(firstname);
txtLastName.setText(lastname);
}
}
}
Re: setLocation Problem when resizing shell [message #450843 is a reply to message #450651] Fri, 18 February 2005 15:08 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Ok, there's too much code for me to fix right now in the late throws of M5.
The problem is that you are using both setBounds() and layout to position
the control. The job of layout is to position the children of a composite
when the composite is resized. That is happening with one of your panels.
If you use resize to resize a child that is also being resized by a layout,
the next time that the composite is resized, the layout comes along and
repositions the child. The key point here is that layout does not take into
account the current position of a control, it just applies the algorithm.
So, either position the new panel using layout or position the children of
the shared parent composite with setBounds() and don't use a layout. Hope
this helps.

"Peter Goetz" <kingofbrain@web.de> wrote in message
news:cuvout$4lh$1@www.eclipse.org...
> Steve Northover wrote:
> > You're going to have to code a small stand alone snippet that shows then
> > problem. I can't understand it from your description.
> >
> > "Peter Goetz" <kingofbrain@web.de> wrote in message
> > [...]
>
> Hi Steve,
>
> thank you very much for your answer. I have prepared a relatively small
> piece of code that illustrates the error.
> It is attached below.
> The important thing happens in line 115 where I call setLocation on the
> Composite.
> If you execute the program and open or close the "fastview", everything
> is fine. But if you resize the shell, the Composite gets the location 0/0.
> I hope you can help me!
> Thanks a lot in advance.
>
> Peter
>
> ===== CODE =====
>
> package de.kaobe.fastview;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.ControlAdapter;
> import org.eclipse.swt.events.ControlEvent;
> import org.eclipse.swt.events.SelectionAdapter;
> import org.eclipse.swt.events.SelectionEvent;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.FormAttachment;
> import org.eclipse.swt.layout.FormData;
> import org.eclipse.swt.layout.FormLayout;
> import org.eclipse.swt.widgets.Button;
> 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;
>
> /**
> * @author pgoetz
> */
> public class ErrorSnippet
> {
> public static void main(String[] args)
> {
> ErrorSnippet snippet = new ErrorSnippet();
> }
>
> public ErrorSnippet()
> {
> Display display = new Display();
> final Shell shell = new Shell(display, SWT.SHELL_TRIM);
> FastView fastview = new FastView(shell, SWT.NONE, "address",
> new TestComposite(shell, SWT.NONE));
> FormData formData;
>
> formData = new FormData(SWT.DEFAULT,
> fastview.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
> formData.top = new FormAttachment(75);
> formData.left = new FormAttachment(25);
> formData.right = new FormAttachment(75);
>
> fastview.setLayoutData(formData);
>
> shell.setLayout(new FormLayout());
> shell.open();
>
> while(!shell.isDisposed())
> {
> while(!display.readAndDispatch())
> {
> display.sleep();
> }
> }
>
> display.dispose();
> }
>
> class FastView extends Composite
> {
> private Label title;
> private Button button;
> private Composite content;
> private boolean isExtended = false;
>
> public FastView(Composite parent, int style, String title,
> Composite content)
> {
> super(parent, style);
>
> this.content = content;
>
> initWidgets(title);
> layoutWidgets();
> pack();
> }
>
> private void changeFastViewState()
> {
> if(isExtended)
> {
> button.setText("open");
> }
> else
> {
> button.setText("close");
> }
>
> isExtended = !isExtended;
>
> content.setVisible(isExtended);
> content.setFocus();
> }
>
> private void initWidgets(String titleText)
> {
> title = new Label(this, SWT.NONE);
> button = new Button(this, SWT.FLAT);
>
> content.setVisible(false);
>
> setBackground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
> addControlListener(new ControlAdapter()
> {
> public void controlResized(ControlEvent e)
> {
> layoutContents();
> }
>
> private void layoutContents()
> {
> int x = getBounds().x;
> int y = getBounds().y - content.getBounds().height;
> int width = getBounds().width;
> int height = content.getBounds().height;
> Rectangle bounds = new Rectangle(x, y, width,
height);
>
> content.setBounds(bounds);
> }
> });
>
> title.setText(titleText);
>
> button.setText("open");
> button.addSelectionListener(new SelectionAdapter()
> {
> public void widgetSelected(SelectionEvent e)
> {
> changeFastViewState();
> }
> });
> }
>
> private void layoutWidgets()
> {
> FormLayout layout = new FormLayout();
> FormData formData;
>
> setLayout(layout);
>
> formData = new FormData();
> formData.top = new FormAttachment(button, 0, SWT.CENTER);
> formData.left = new FormAttachment(5);
> formData.right = new FormAttachment(button);
> title.setLayoutData(formData);
>
> formData = new FormData();
> formData.top = new FormAttachment(0);
> formData.left = new FormAttachment(50,
> -button.computeSize(SWT.DEFAULT, SWT.DEFAULT).x / 2);
> button.setLayoutData(formData);
> }
> }
>
> class TestComposite extends Composite
> {
> private Label lblFirstName;
> private Label lblLastName;
> private Text txtFirstName;
> private Text txtLastName;
>
> public TestComposite(Composite parent, int style)
> {
> super(parent, style);
>
> setBackground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
>
> initWidgets();
> layoutWidgets();
> fillWidgets("Peter", "Goetz");
> pack();
> }
>
> private void initWidgets()
> {
> lblFirstName = new Label(this, SWT.NONE);
> lblLastName = new Label(this, SWT.NONE);
> txtFirstName = new Text(this, SWT.BORDER);
> txtLastName = new Text(this, SWT.BORDER);
>
> lblFirstName.setText("firstname:");
> lblLastName.setText("lastname:");
> }
>
> private void layoutWidgets()
> {
> FormData formData;
> FormLayout layout = new FormLayout();
> int broadMargin = 5;
>
> layout.marginWidth = layout.marginHeight = 10;
>
> this.setLayout(layout);
>
> // first row
> formData = new FormData(100, SWT.DEFAULT);
> formData.top = new FormAttachment(0);
> formData.left = new FormAttachment(0);
> formData.right = new FormAttachment(50);
> lblFirstName.setLayoutData(formData);
>
> formData = new FormData(100, SWT.DEFAULT);
> formData.top = new FormAttachment(lblFirstName, 0,
SWT.CENTER);
> formData.left = new FormAttachment(lblFirstName);
> formData.right = new FormAttachment(100);
> lblLastName.setLayoutData(formData);
>
> formData = new FormData();
> formData.top = new FormAttachment(lblFirstName);
> formData.left = new FormAttachment(lblFirstName, 0,
SWT.LEFT);
> formData.right = new FormAttachment(lblFirstName, 0,
> SWT.RIGHT);
> txtFirstName.setLayoutData(formData);
>
> formData = new FormData();
> formData.top = new FormAttachment(lblLastName);
> formData.left = new FormAttachment(lblLastName, 0, SWT.LEFT);
> formData.right = new FormAttachment(lblLastName, 0,
SWT.RIGHT);
> txtLastName.setLayoutData(formData);
> }
>
> private void fillWidgets(String firstname, String lastname)
> {
> txtFirstName.setText(firstname);
> txtLastName.setText(lastname);
> }
> }
> }
Re: setLocation Problem when resizing shell [message #451006 is a reply to message #450843] Mon, 21 February 2005 08:28 Go to previous message
Peter Goetz is currently offline Peter GoetzFriend
Messages: 38
Registered: July 2009
Member
Steve Northover wrote:
> Ok, there's too much code for me to fix right now in the late throws of M5.
> The problem is that you are using both setBounds() and layout to position
> the control. The job of layout is to position the children of a composite
> when the composite is resized. That is happening with one of your panels.
> If you use resize to resize a child that is also being resized by a layout,
> the next time that the composite is resized, the layout comes along and
> repositions the child. The key point here is that layout does not take into
> account the current position of a control, it just applies the algorithm.
> So, either position the new panel using layout or position the children of
> the shared parent composite with setBounds() and don't use a layout. Hope
> this helps.
>

Hi Steve,

thank you very much, that is the explanation I was looking for. So I can
fix the code myself by not layouting widgets via a Layout when I want to
position them myself.
I'm looking forward to eclipse 3.1, so I don't want to hold you up in
implementing... ;)
Thank you!!

Peter
Previous Topic:how to see the updated tree
Next Topic:Couldn't find per display information in Solaris
Goto Forum:
  


Current Time: Sat Sep 19 13:13:43 GMT 2020

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

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

Back to the top