java - How to create toolbar with left, center and right sections in javaFX? -
i'm trying create customized toolbar in javafx. toolbar should able display controls in center, in left side , in right side (three sections) of surface. problem have no idea achive this. read many hints related problem, dont work me or i'm doing wrong...
anyway wrote several methods, represent different ways implement toolbar no of them works properly. here have attempts:
using hbox hgrow property spring. didn't work.
public toolbar createtoolbar() { toolbar toolbar = new toolbar(); pane emptypane = new pane(); hbox spring = new hbox(emptypane); spring.sethgrow(emptypane, priority.always); toolbar.getitems().addall(spring, new label("label")); return toolbar; }
2.it works left , right section, how define center one?
public anchorpane createtoolbar2() { anchorpane toolbar = new anchorpane(); label leftlabel = new label("left"); label rightlabel = new label("right"); toolbar.getchildren().addall(leftlabel, rightlabel); toolbar.setleftanchor(leftlabel, 0.0); toolbar.setrightanchor(rightlabel, 0.0); return toolbar; }
this approach works layout, cannot listen events left , center section because covered right section (stackpane reason), solution useless.
public stackpane createtoolbar3() { stackpane toolbar = new stackpane(); button left = new button("left button"); button right = new button("right button"); button center = new button("center button"); hbox leftsection = new hbox(left); leftsection.setalignment(pos.center_left); hbox centersection = new hbox(center); centersection.setalignment(pos.center); hbox rightsection = new hbox(right); rightsection.setalignment(pos.center_right); toolbar.getchildren().addall(leftsection, centersection, rightsection); left.setonaction(event -> system.out.println("left")); right.setonaction(event -> system.out.println("right")); center.setonaction(event -> system.out.println("center")); return toolbar; }
above methods i'm invoking in code block:
@override public void start(stage stage) { borderpane borderpane = new borderpane(); borderpane.setprefwidth(500); borderpane.setprefheight(300); borderpane.settop(createtoolbar4()); stage.setscene(new scene(borderpane)); stage.show(); }
i grateful in matter.
simply use hbox instead of stackpane. stackpane puts nodes on top of each other.
see modified example of third attempt.
another option use fx toolbar, similar how jewelsea mentioned:
both attempts should flexible enough needs. differ in how control on layout , how many features can inherit standard toolbar.
Comments
Post a Comment