Java SWT-如果新的子级被添加到组合中,请倾听?

Java SWT-如果新的子级被添加到组合中,请倾听?,java,events,user-interface,swt,Java,Events,User Interface,Swt,我有一个组合。 在SWT中,儿童由儿童添加,不由父母添加: Composite parent = new Composite(shell, SWT.none); Composite child = new Composite(parent, SWT.none); 这意味着,在父母的内心,我不知道孩子是以何种方式和何时被添加到他们的父母中的 问题: 在父对象内部,我需要知道何时将新的子对象添加到父对象。 是否有任何方法可以在ChildAndListener上注册或任何要覆盖的add()方法?添加

我有一个
组合
。 在SWT中,儿童由儿童添加,不由父母添加:

Composite parent = new Composite(shell, SWT.none);
Composite child = new Composite(parent, SWT.none);
这意味着,在父母的内心,我不知道孩子是以何种方式和何时被添加到他们的父母中的

问题:

在父对象内部,我需要知道何时将新的子对象添加到父对象。
是否有任何方法可以在ChildAndListener上注册
或任何要覆盖的
add()
方法?

添加自定义
MyComposite
并创建自定义
ChildEvent
以及
ChildEventListener
接口的唯一方法
MyComposite
能够注册
ChildEvent
的侦听器,并在创建子
组合时触发此事件

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.TypedEvent;
import org.eclipse.swt.internal.SWTEventListener;
import org.eclipse.swt.widgets.*;

/*
 * This example demonstrates the minimum amount of code required
 * to open an SWT Shell and process the events.
 */
public class HelloWorld1 {

public static void main (String [] args) {
    Display display = new Display ();
    Shell shell = new HelloWorld1 ().open (display);
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}

public Shell open (Display display) {
    Shell shell = new Shell (display);
    MyComposite parent = new MyComposite(shell, SWT.NONE);
    parent.addChildEventListener(new ChildEventListener() {

        @Override
        public void add(ChildEvent e) {
            System.out.println("Child added.");

        }
    });
    MyComposite child = new MyComposite(parent, SWT.NONE);

//  shell.open ();
    return shell;
}
}

class MyComposite extends Composite {

    ChildEventListener[] childEventListeners = new ChildEventListener[0];

    public MyComposite(Composite parent, int style) {
        super(parent, style);
        if (parent instanceof MyComposite){
            ((MyComposite)parent).fireChildEvent(new ChildEvent(this));
        }
    }
    public void addChildEventListener (ChildEventListener listener) {
        checkWidget();
        if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
        ChildEventListener[] newChildEventListeners = new ChildEventListener[childEventListeners.length + 1];
        System.arraycopy(childEventListeners, 0, newChildEventListeners, 0, childEventListeners.length);
        childEventListeners = newChildEventListeners;
        childEventListeners[childEventListeners.length - 1] = listener;
    }
    public void removeChildEventListener (ChildEventListener listener) {
        if (childEventListeners.length == 0) return;
        int index = -1;
        for (int i = 0; i < childEventListeners.length; i++) {
            if (listener == childEventListeners[i]){
                index = i;
                break;
            }
        }
        if (index == -1) return;
        if (childEventListeners.length == 1) {
            childEventListeners = new ChildEventListener[0];
            return;
        }
        ChildEventListener[] newChildEventListeners = new ChildEventListener[childEventListeners.length - 1];
        System.arraycopy (childEventListeners, 0, newChildEventListeners, 0, index);
        System.arraycopy (childEventListeners, index + 1, newChildEventListeners, index, childEventListeners.length - index - 1);
        childEventListeners = newChildEventListeners;
    }
    public void fireChildEvent(ChildEvent event){
        for (int i = 0; i < childEventListeners.length; i++) {
            childEventListeners[i].add (event);
        }

    }
}

interface ChildEventListener extends SWTEventListener {
    public void add(ChildEvent e);
}

class ChildEvent extends TypedEvent {

    public ChildEvent(Widget widget) {
        super(widget);
        // TODO Auto-generated constructor stub
    }

}
import org.eclipse.swt.swt;
导入org.eclipse.swt.events.TypedEvent;
导入org.eclipse.swt.internal.SWTEventListener;
导入org.eclipse.swt.widgets.*;
/*
*此示例演示所需的最小代码量
*打开SWT外壳并处理事件。
*/
公共类HelloWorld1{
公共静态void main(字符串[]args){
显示=新显示();
Shell=newhelloworld1().open(display);
而(!shell.isDisposed()){
如果(!display.readAndDispatch())display.sleep();
}
display.dispose();
}
公共外壳打开(显示){
外壳=新外壳(显示);
MyComposite父代=新的MyComposite(shell,SWT.NONE);
addChildEventListener(新的ChildEventListener(){
@凌驾
公共无效添加(儿童事件){
System.out.println(“儿童添加”);
}
});
MyComposite子级=新的MyComposite(父级,SWT.NONE);
//shell.open();
返回壳;
}
}
类MyComposite扩展了复合{
ChildEventListener[]childEventListeners=新的ChildEventListener[0];
公共MyComposite(复合父级,int样式){
超级(父母,风格);
if(MyComposite的父实例){
((MyComposite)父项).fireChildEvent(新的ChildEvent(this));
}
}
public void addChildEventListener(ChildEventListener侦听器){
checkWidget();
if(listener==null)SWT.error(SWT.error\u null\u参数);
ChildEventListener[]newChildEventListeners=new ChildEventListener[childEventListeners.length+1];
arraycopy(childEventListeners,0,newChildEventListeners,0,childEventListeners.length);
childEventListeners=newChildEventListeners;
childEventListeners[childEventListeners.length-1]=侦听器;
}
public void removeChildEventListener(ChildEventListener侦听器){
if(childEventListeners.length==0)返回;
int指数=-1;
for(int i=0;i
我想知道添加孩子的目的是为了布局孩子 (添加
LayoutData
例如
GridData
)将子项添加到使用特定布局(例如
GridLayout
)进行布局的子项时

由于无法在添加时布局子项-请在每个小部件上检查它们的布局:

    /*
     * To make the forms fill the group - laying out the children is necessary.
     * Unfortunately it is not possible to listen for children addition, in order to layout the children addition automatically.
     * This means that the user will have to remember  to layout the children, which is a nogo.
     * 
     * Solution: 
     * istead of adding right layout on child addition -  check if there are new children every time the widget is resized.
     * if a widget without layoutData is found (new child not layed out) - add a layout
     */
    this.addControlListener(new ControlAdapter() {
        @Override
        public void controlResized(ControlEvent e) {
            layoutChildren();
        }
    });
}
private void layoutChildren(){
    for(Control child:getChildren()){
        if(child.getLayoutData() == null){
            child.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 
        }
    }
}

public void addToGroup(Composite child){
    child.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
}

必须为添加的第一项指定“活动”状态。对于我自己的应用程序来说,答案似乎是实现这一目标的唯一途径。基本上,听
SWT.Resize
events…顺便说一句:如果你自己添加组件,为什么不手动将第一个组件设置为“Active”呢?因为这将是需要考虑的另一件事,还有一个错误源Hi,我来自一年后。我想谢谢你!罗曼C:这个解决方案对我来说似乎毫无用处。因为它只能在将
MyComposite
对象添加到其他
MyComposite
s时通知家长,而不能在添加任何标准控件时通知家长。我真的认为你应该在解决方案文本中提到这个限制。