java - SWT Button Dropdown Control -
is there standard swt control resembles button displays arrow , opens dropdown menu when pressed , not toolbar-only control?
it this:
it similar combo box control, except "button" area act more actual button - text not change based on selection, appear depressed when clicked, , items used actions or navigational purposes instead of selection. it's similar control available toolbars, need use on regular composite instead.
this doable using regular button , popup-menu controls - however, not believe can display arrow next text on button way. anyway, since kind of control seems common, assumed there standard way use these 2 things one.
i think, should drop down menu behavior
- create
menu
styleswt.drop_down
- create
menuitems
onmenu
if want button
- create button style
swt.arrow | swt.down
- add
selectionlistener
- in
selectionlistener
, create menu styleswt.pop_up
, position menu @ 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(); }
Comments
Post a Comment