java - Javafx: bind Hbox spacing? -
i have tableview
, hbox
below table, in hbox there threelabel
s 1 contains text, , 2 contains sum of 2 columns in table. want set spacing somehow dynamic hbox align 2 labels below 2 columns in table belong. there possibility bind hbox
's spacing columns position. accept other solution fixes labels below respective columns. here image shows want:
bind each label's minwidth
, prefwidth
properties corresponding column's width
property:
import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.layout.borderpane; import javafx.scene.layout.hbox; import javafx.stage.stage; public class tablewithlabelsbelowcolumns extends application { @override public void start(stage primarystage) { tableview<void> table = new tableview<>(); table.getitems().add(null); tablecolumn<void, void> firstnamecolumn = new tablecolumn<>("first name"); tablecolumn<void, void> lastnamecolumn = new tablecolumn<>("last name"); tablecolumn<void, void> emailcolumn = new tablecolumn<>("email"); table.getcolumns().add(firstnamecolumn); table.getcolumns().add(lastnamecolumn); table.getcolumns().add(emailcolumn); label fnlabel = new label("fn"); label lnlabel = new label("ln"); label emaillabel = new label("e"); fnlabel.prefwidthproperty().bind(firstnamecolumn.widthproperty()); fnlabel.minwidthproperty().bind(firstnamecolumn.widthproperty()); lnlabel.prefwidthproperty().bind(lastnamecolumn.widthproperty()); lnlabel.minwidthproperty().bind(lastnamecolumn.widthproperty()); emaillabel.prefwidthproperty().bind(emailcolumn.widthproperty()); emaillabel.minwidthproperty().bind(emailcolumn.widthproperty()); hbox labels = new hbox(fnlabel, lnlabel, emaillabel); borderpane root = new borderpane(table); root.setbottom(labels); scene scene = new scene(root); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }
an alternative approach subclass pane
, override layoutchildren()
method position labels according widths of columns. register listener request layout on pane each column's width:
import javafx.application.application; import javafx.beans.value.changelistener; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.layout.borderpane; import javafx.scene.layout.pane; import javafx.stage.stage; public class tablewithlabelsbelowcolumns extends application { @override public void start(stage primarystage) { tableview<void> table = new tableview<>(); table.getitems().add(null); tablecolumn<void, void> firstnamecolumn = new tablecolumn<>("first name"); tablecolumn<void, void> lastnamecolumn = new tablecolumn<>("last name"); tablecolumn<void, void> emailcolumn = new tablecolumn<>("email"); table.getcolumns().add(firstnamecolumn); table.getcolumns().add(lastnamecolumn); table.getcolumns().add(emailcolumn); label fnlabel = new label("fn"); label lnlabel = new label("ln"); label emaillabel = new label("e"); pane labelpane = new pane(fnlabel, lnlabel, emaillabel) { @override protected void layoutchildren() { double fnwidth = firstnamecolumn.getwidth(); double fnheight = fnlabel.prefheight(fnwidth); fnlabel.resizerelocate(0, 0, fnwidth, fnheight); double lnwidth = lastnamecolumn.getwidth(); double lnheight = lnlabel.prefheight(lnwidth); lnlabel.resizerelocate(fnwidth, 0, lnwidth, lnheight); double emailwidth = emailcolumn.getwidth(); double emailheight = emaillabel.prefheight(emailwidth); emaillabel.resizerelocate(fnwidth+lnwidth, 0, emailwidth, emailheight); } }; changelistener<? super number> listener = (obs, oldvalue, newvalue) -> labelpane.requestlayout(); firstnamecolumn.widthproperty().addlistener(listener); lastnamecolumn.widthproperty().addlistener(listener); emailcolumn.widthproperty().addlistener(listener); borderpane root = new borderpane(table); root.setbottom(labelpane); scene scene = new scene(root); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }
Comments
Post a Comment