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

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

问题描述

是否有标准SWT控件类似于按钮显示箭头打开下拉菜单当被按下并且不是仅工具栏控件

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?

这将是这样的:

它类似于组合框控件,除了按钮区域将与实际按钮更相似的行为 - 其文本将不会根据您的选择而改变,当单击时它将显示为按压,并且项目将用于操作或导航目的,而不是选择。它也类似于可用于工具栏的控件,但是我需要使用常规复合代替。

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.

推荐答案

p>我想,这是你应该做的下拉菜单行为

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


  1. 创建菜单与样式 SWT.DROP_DOWN

  2. 在$ $上创建 MenuItems c $ c>菜单

  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天全站免登陆