0

私はSWTアプリケーション(シェル)をいくつかのコンポジットで持っています:メニューバーはコンポジット内にあり、さまざまな要素を持つ2番目のコンポジットもあります。

メニュー バーのアクション リスナーから、2 番目のコンポジットの要素にアクセスしたいと思います。

現在、これを行うことでコンポジットを見つけています。より良い/より簡単な方法はありますか?

Control[] appComposites;
appComposites = parent.getChildren();
Composite remoteFile;
for(int i=0; i<appComposites.length;i++){
    if(appComposites[i].toString().compareTo("RemoteFile {}") == 0){
        remoteFile = (Composite) appComposites[i];
    }
}
//Now I must make another loop inside remoteFile to find the element that I need ...
4

1 に答える 1

0

メソッドを使用して、 sWidget#setData(Object)に識別情報を追加できますWidget

次に例を示します。

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(2, true));

    Listener listener = new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            Widget widget = arg0.widget;

            System.out.println(widget.getData().equals("Button 1");
        }
    };

    for(int i = 0; i < 10; i++)
    {
        Button button = new Button(shell, SWT.PUSH);
        button.setText("Button " + i);
        button.setData("Button " + i);

        button.addListener(SWT.Selection, listener);
    }

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

Widget見ているものが探しているものかどうかを調べるには、次を使用できます。

widget.getData().equals("YOUR IDENTIFIER");
于 2013-07-07T12:48:46.000 に答える