SWT中调用SetLayout 遇到ClassCastException: GridData cannot be cast to RowData 问题调查

来源:互联网 发布:广东11选5当前遗漏数据 编辑:程序博客网 时间:2024/06/06 17:09


问题

在华能客户端开发过程中,使用SWT 开发的GUI, 在修改代码将其中的一个SashForm 改为Composite 之后,运行程序会遇到如下的Exception.

[plain] view plaincopy
  1.   
[plain] view plaincopy
  1. java.lang.ClassCastException: org.eclipse.swt.layout.GridData cannot be cast to org.eclipse.swt.layout.RowData  
  2.     at org.eclipse.swt.layout.RowLayout.layoutVertical(RowLayout.java:367)  
  3.     at org.eclipse.swt.layout.RowLayout.computeSize(RowLayout.java:206)  
  4.     at org.eclipse.swt.widgets.Composite.computeSize(Composite.java:231)  
  5.     at org.eclipse.swt.layout.FillData.computeSize(FillData.java:26)  
  6.     at org.eclipse.swt.layout.FillLayout.computeChildSize(FillLayout.java:151)  


查找原因

从最后改动的代码中没有找到可能出错的地方,于是Google搜索如下语句: java.lang.ClassCastException: org.eclipse.swt.layout.GridData cannot be cast toorg.eclipse.swt.layout.RowData

看到这个网页:http://www.eclipse.org/forums/index.php/mv/msg/36739/119448/

其中有如下的讨论:

you can't be surethat the layout of the parent composite is a 
GridLayout. And using a GridData together with a nonGridLayout (eg. 
FillLayout as in your case) will always fail at runtime(same goes for SWT).

结合这个实例,猜测可能与GridData 有关,搜索代码看到的确有SetLayoutData(new GridData(…))的调用,具体的Widget 关系如下。括号内为Layout, 箭头方向为父控件->子控件。

Container(GridLayout)->SashForm->SashForm->TabFolder(FillLayout)->SashForm->Table

代码中有上面的Table 调用setLayoutData(new GridData(GridData.FILL_BOTH)) . 尝试取消这个调用,问题就不重现了。

解决方法

将前面的TabFolder 的Layout 由FillLayout 改为GridLayout. 问题解决,不再重现。

原创粉丝点击