Is it possible to put FormLayout into GridLayout?

梦想的初衷 提交于 2020-01-06 03:27:05

问题


I am trying to put a FormLayout composite into a GridLayout grid, but i get an exception. Am i doing something wrong or is it just not possible? Here is my code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class formlayout {
    public static void main(String[] args)
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        GridLayout layout= new GridLayout(1, false);
        shell.setLayout(layout);

        Composite inputs = new Composite(shell, SWT.NONE);
        inputs.setLayout(new FormLayout());
        FormData fd1 = new FormData();
        fd1.left = new FormAttachment(0, 0);
        fd1.right = new FormAttachment(100,0);
        inputs.setLayoutData(fd1);

        Button button1 = new Button(shell, SWT.PUSH);
        button1.setText("B1");
        button1.setLayoutData(new FormData());
        FormData formData = new FormData();
        formData.left = new FormAttachment(20,0);
        formData.right = new FormAttachment(100,0);
        button1.setLayoutData(formData);

        Button button2 = new Button(shell, SWT.PUSH);
        button2.setText("B2");
        button2.setLayoutData(new FormData());
        FormData formData2 = new FormData();
        formData2.left = new FormAttachment(0,0);
        formData2.right = new FormAttachment(20,0);
        button2.setLayoutData(formData2);    

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

The exception i get is:

Exception in thread "main" java.lang.ClassCastException: org.eclipse.swt.layout.FormData cannot be cast to org.eclipse.swt.layout.GridData

This is of course just a sample script to demonstrate the problem. Actually shell is defined lower in the code and it is set as GridLayout so i cant really change that but i still need to use FormLayout to acchive my goal with buttons.


回答1:


In this code:

Composite inputs = new Composite(shell, SWT.NONE);
inputs.setLayout(new FormLayout());
FormData fd1 = new FormData();
fd1.left = new FormAttachment(0, 0);
fd1.right = new FormAttachment(100,0);
inputs.setLayoutData(fd1);

you are setting FormData in the layout data for inputs. The layout data is used by the layout specified for the parent of the control - in this case the parent is shell which is using GridLayout.

So when the grid layout for shell is doing the layout it is expecting all its children to have a GridData in the layout data, but you have FormData so the cast it is doing fails.

Specify GridData for the layout data of inputs, just use FormData for the child controls of inputs.



来源:https://stackoverflow.com/questions/28761207/is-it-possible-to-put-formlayout-into-gridlayout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!