SWT按钮下拉控件 [英] SWT Button Dropdown Control

查看:190
本文介绍了SWT按钮下拉控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个标准SWT类似于一个按钮其中控制显示一个箭头打开一个下拉菜单当pressed是的不是纯工具栏控制

Is there a standard SWT control that resembles a button which displays an arrow and opens a dropdown menu when pressed and is not a toolbar-only control?

这将是这样的:

这是一个类似组合框控件,除了按钮区域将更多采取类似行动,以实际的按钮 - 它的文本不会改变根据您的选择,点击后会出现去pressed和的项目将被用于操作或导航目的,而不是选择。这也类似于提供工具栏控制,但我需要用它定期复合吧。

It is similar to a combo box control, except that the "button" area would act more similarly to an actual button - its text would not change based on your selection, it would appear depressed when clicked, and the items would be used for actions or navigational purposes instead of for selection. It's also similar to a control available for toolbars, but I need to use it on a regular composite instead.

这是只需使用常规的按钮,弹出菜单控制几乎是可行的 - 但是,我不相信我可以在旁边这样按钮上的文本显示的箭头。无论如何,因为这种控制似乎是相当普遍的,我以为会有一个标准的方式来使用这两个东西之一。

This is nearly doable simply by using regular button and popup-menu controls - however, I do not believe I can display the arrow next to the text on the button this way. Anyway, since this kind of control seems fairly common, I assumed there would be a standard way to use these two things as one.

推荐答案

我想,这是你应该做得到下拉菜单的行为是什么

I think, this is what you should do get Drop down menu behavior


  1. 创建菜单风格 SWT.DROP_DOWN

  2. 创建的MenuItems 菜单

  1. Create Menu with style SWT.DROP_DOWN
  2. Create MenuItems on Menu

如果你想有一个按钮


  1. 风格创建一个按钮 SWT.ARROW | SWT.DOWN

  2. 添加 SelectionListener中

  3. SelectionListener中,创建风格菜单 SWT.POP_UP 和按钮位置位置的菜单。

  1. Create a Button with style SWT.ARROW | SWT.DOWN
  2. add SelectionListener
  3. In SelectionListener, Create a Menu with style SWT.POP_UP and position the menu at the button location.

// code

public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setSize(300, 200);
        shell.setText("Button Example");
        shell.setLayout(new RowLayout());


        /**
         * 
         * Approach1
         * 
         */
        final Composite btnCntrl = new Composite(shell, SWT.BORDER);
        btnCntrl.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        btnCntrl.setBackgroundMode(SWT.INHERIT_FORCE);
        GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).spacing(0, 1).applyTo(btnCntrl);
        CLabel lbl = new CLabel(btnCntrl, SWT.NONE);
        lbl.setText("Animals");
        Button btn = new Button(btnCntrl, SWT.FLAT|SWT.ARROW|SWT.DOWN);
        btn.setLayoutData(new GridData(GridData.FILL_VERTICAL));

        btn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                Menu menu = new Menu(shell, SWT.POP_UP);

                MenuItem item1 = new MenuItem(menu, SWT.PUSH);
                item1.setText("Hare");
                MenuItem item2 = new MenuItem(menu, SWT.PUSH);
                item2.setText("Fox");
                MenuItem item3 = new MenuItem(menu, SWT.PUSH);
                item3.setText("Pony");



                Point loc = btnCntrl.getLocation();
                Rectangle rect = btnCntrl.getBounds();

                Point mLoc = new Point(loc.x-1, loc.y+rect.height);

                menu.setLocation(shell.getDisplay().map(btnCntrl.getParent(), null, mLoc));

                menu.setVisible(true);
            }
        });



        /***
         * 
         * 
         * Approach 2
         * 
         */


        final Composite btnCntrl2 = new Composite(shell, SWT.BORDER);
        btnCntrl2.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        btnCntrl2.setBackgroundMode(SWT.INHERIT_FORCE);
        GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).spacing(0, 1).applyTo(btnCntrl2);
        CLabel lbl2 = new CLabel(btnCntrl2, SWT.NONE);
        lbl2.setText("Animals");
        Button btn2 = new Button(btnCntrl2, SWT.FLAT|SWT.ARROW|SWT.DOWN);
        btn2.setLayoutData(new GridData(GridData.FILL_VERTICAL));

        btn2.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);
                Shell menu = (Shell) btnCntrl2.getData("subshell");
                if(menu != null && !menu.isDisposed()){
                    menu.dispose();
                }
                menu = new Shell(shell, SWT.NONE);
                menu.setLayout(new FillLayout());
                Table table = new Table(menu, SWT.FULL_SELECTION);
                table.addListener(SWT.MeasureItem, new Listener() {

                    @Override
                    public void handleEvent(Event event) {
                        event.height = 20; //TODO: determine later
                    }
                });

                table.addListener(SWT.PaintItem, new Listener() {

                    @Override
                    public void handleEvent(Event event) {
                        Rectangle bounds = event.getBounds();
                        event.gc.setBackground(event.display.getSystemColor(SWT.COLOR_BLUE));
                        event.gc.drawLine(bounds.x, bounds.y+bounds.height-1, bounds.x+bounds.width, bounds.y+bounds.height-1);
                    }
                });
                TableItem tableItem= new TableItem(table, SWT.NONE);
                tableItem.setText(0, "Hare");

                TableItem tableItem2= new TableItem(table, SWT.NONE);
                tableItem2.setText(0, "Pony" );

                TableItem tableItem3= new TableItem(table, SWT.NONE);
                tableItem3.setText(0, "Dog");


                Point loc = btnCntrl2.getLocation();
                Rectangle rect = btnCntrl2.getBounds();

                Point mLoc = new Point(loc.x, loc.y+rect.height);

                menu.setLocation(shell.getDisplay().map(btnCntrl2.getParent(), null, mLoc));
                menu.pack();
                menu.setVisible(true);

                btnCntrl2.setData("subshell", menu);

            }
        });

        display.addFilter(SWT.MouseDown, new Listener() {

            @Override
            public void handleEvent(Event event) {
                Shell shell = (Shell) btnCntrl2.getData("subshell");
                if(shell != null && !shell.getBounds().contains(event.display.map((Control)event.widget, null, new Point(event.x, event.y)))){
                    shell.dispose();
                    btnCntrl2.setData("subshell", null);
                }
            }
        });

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

这篇关于SWT按钮下拉控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆