Home » Eclipse Projects » Standard Widget Toolkit (SWT) » setLocation Problem when resizing shell
| |
Re: setLocation Problem when resizing shell [message #450651 is a reply to message #450646] |
Wed, 16 February 2005 15:29 |
Peter Goetz 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 |
Steve Northover 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);
> }
> }
> }
|
|
| |
Goto Forum:
Current Time: Fri Sep 20 09:11:27 GMT 2024
Powered by FUDForum. Page generated in 0.04340 seconds
|