SWT Button Dropdown Control
Asked Answered
G

3

10

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 would be something like this:

enter image description here

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.

Genuflection answered 19/4, 2013 at 20:52 Comment(0)
F
12

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

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

if you want a button

  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();
      }
Feudalism answered 23/4, 2013 at 18:29 Comment(4)
Unfortunately, I am actively trying to avoid using menu- or toolbar-based controls. I am trying to use something that works normally on a composite.Genuflection
I have tried "ARROW | DOWN" before, and it seems to be very lacking. It is not compatible with neither text nor images, from what I can see.Genuflection
How about creating your own composite with Label and Button (arrow , setimage )!! that doesnt work?Feudalism
Not that I know of. Unless I am doing it a different way than you intended, such a construct looks nothing like a natural control. If you believe it can be done, please post as a detailed answer with screenshots.Genuflection
D
0

This snippet shows how to use the described widget in a SWT toolbar. You can set the button text by using the item.setText() method.

Debroahdebs answered 20/4, 2013 at 12:50 Comment(0)
F
0

This question is almost 10 years old, but just in case someone is still looking for a solution (like I just did ;) ):

I achieved a pretty close behaviour of your description using only a Button and a Menu using this approach: http://eclipseo.blogspot.com/2012/07/show-context-menu-programmatically.html

Button button = new Button(parent, SWT.PUSH);
button.setText("Animals");

Menu menu = new Menu(button);
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText("hare");

menu.addListener(SWT.Show, new Listener() {
    @Override
    public void handleEvent(Event event) {
        menu.setVisible(true);
    }
});

button.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
        menu.notifyListeners(SWT.Show, null);
    }
});

The result is that the menu is shown when you (left) click on the button.

Bonus: to achieve the expand icon at the end, you can add a unicode character for a down triangle in the button text like so:

button.setText("Animals \u2BC6");

HTH, Ben

Finality answered 9/12, 2022 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.