Montag, 9. August 2010

Dropdown Button for SWT

Ever come to that point you needed a button with an menu that pops up after clicking on it? Same like eclipse has in its toolbars? Yes? So i did. But after doing a bit research, nothing suitable came across. So once again i decided to write it myself.

I wanted the behaviour as shown on the picture. You have an ordinary button (with his whole functionallity) and a menu popup.

The code is finished and ready to use. Just use at your own risk ;-)
http://download.planethost.de/code/DropdownButton.java






Update:
A short snippet for the shell shown above:

public class Snippet {

public static void main (String [] args) {
    Display display = new Display ();
    Shell shell = new Shell (display);
    shell.setLayout (new RowLayout ());
    
    SelectionListener sl = new SelectionListener()
        {
        
        @Override
        public void widgetSelected(SelectionEvent e)
            {
            MenuItem mi = (MenuItem)e.getSource();
            System.out.println(mi.getText() + " selected");
            }
        
        @Override
        public void widgetDefaultSelected(SelectionEvent e)
            {
            }
    };
    
    DropdownButton db = new DropdownButton(shell, "myDropButton", SWT.NONE);
    db.addMenuItem("item 1").addSelectionListener(sl);
    db.addMenuItem("item 2").addSelectionListener(sl);
    db.addMenuItem("item 3").addSelectionListener(sl);
    
    shell.pack ();
    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}
} 

Keine Kommentare:

Kommentar veröffentlichen