2

ユーザーがこのボタンをクリックしたときに、ツールバー ボタンの下にポップアップメニューを表示したいと考えています。SWT.DROP_DOWNのスタイルについて読んだことがありますが、このサンプルToolItemによると、これはアイテムの単純なリストに非常に限定されているようです。代わりに、チェックボックスやラジオ ボタンのメニュー項目などを含むポップアップ メニューを表示したいと考えています。

4

1 に答える 1

8

スタイル SWT.CHECK、SWT.CASCADE、SWT.PUSH、SWT.RADIO、SWT.SEPARATOR を使用して MenuItem を作成できます。javadoc を参照してください。

したがって、このようなツールバー項目のドロップダウンの選択にswtメニューを「ハング」させることができます

public class Test {

private Shell shell;

public Test() {
    Display display = new Display();
    shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FillLayout(SWT.VERTICAL));
    shell.setSize(50, 100);

    ToolBar toolbar = new ToolBar(shell, SWT.FLAT);
    ToolItem itemDrop = new ToolItem(toolbar, SWT.DROP_DOWN);
    itemDrop.setText("drop menu");

    itemDrop.addSelectionListener(new SelectionAdapter() {

        Menu dropMenu = null;

        @Override
        public void widgetSelected(SelectionEvent e) {
            if(dropMenu == null) {
                dropMenu = new Menu(shell, SWT.POP_UP);
                shell.setMenu(dropMenu);
                MenuItem itemCheck = new MenuItem(dropMenu, SWT.CHECK);
                itemCheck.setText("checkbox");
                MenuItem itemRadio = new MenuItem(dropMenu, SWT.RADIO);
                itemRadio.setText("radio1");
                MenuItem itemRadio2 = new MenuItem(dropMenu, SWT.RADIO);
                itemRadio2.setText("radio2");
            }

            if (e.detail == SWT.ARROW) {
                // Position the menu below and vertically aligned with the the drop down tool button.
                final ToolItem toolItem = (ToolItem) e.widget;
                final ToolBar  toolBar = toolItem.getParent();

                Point point = toolBar.toDisplay(new Point(e.x, e.y));
                dropMenu.setLocation(point.x, point.y);
                dropMenu.setVisible(true);
            } 

        }

    });

    shell.open();

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

    display.dispose();
}

public static void main(String[] args) {
    new Test();
}

}
于 2011-06-13T08:53:56.050 に答える