gridpane - Javafx grid layout leaving big spaces -
i making text based game gui. using javafx trying line elements using grid panes. trying making this: example.
here code , looks @ moment
import javafx.application.application; import static javafx.application.application.launch; import javafx.geometry.insets; import javafx.geometry.vpos; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.control.listview; import javafx.scene.control.textarea; import javafx.scene.layout.gridpane; import javafx.stage.stage; public class test extends application { stage window; listview<string> listview; textarea descarea = new textarea(); label healthlabel = new label("health:"); label manalabel = new label("mana:"); label healthdisplay = new label("100/100"); label manadisplay = new label("100/100"); public static void main(string[] args) { launch(args); } @override public void start(stage primarystage) throws exception { window = primarystage; window.settitle("game"); gridpane grid = new gridpane(); grid.setpadding(new insets(10, 10, 10, 10)); grid.setvgap(10); grid.sethgap(10); descarea.setprefwidth(750); descarea.setprefheight(450); descarea.setwraptext(true); gridpane.setconstraints(descarea, 0, 0); gridpane.setconstraints(healthlabel, 1, 0); gridpane.setvalignment(healthlabel, vpos.top); gridpane.setconstraints(healthdisplay, 2, 0); gridpane.setvalignment(healthdisplay, vpos.top); gridpane.setconstraints(manalabel, 1, 1); gridpane.setvalignment(manalabel, vpos.top); gridpane.setconstraints(manadisplay, 2, 1); gridpane.setvalignment(manadisplay, vpos.top); listview = new listview<>(); listview.getitems().addall("action 1", "action 2", "action 3", "action 4"); listview.setprefwidth(260); listview.setprefheight(150); gridpane.setvalignment(listview, vpos.baseline); gridpane.setconstraints(listview, 0, 2); grid.getchildren().addall(descarea, healthlabel, healthdisplay, manalabel, manadisplay, listview); scene scene = new scene(grid, 950, 750); window.setscene(scene); window.show(); }
and shows example2 trying move mana under health label , make list box smaller. grid right way or there better way?
thanks
i trying move mana under health label
let descarea
span 2 rows. mean 2 labels lie in 1 row each (rows 0 , 1) , description area spans both rows:
// gridpane.setconstraints(descarea, 0, 0); // (node, columnindex, rowindex, columnspan, rowspan): gridpane.setconstraints(descarea, 0, 0, 1, 2);
and make list box smaller
you set preferred size of list box with
listview.setprefwidth(260); listview.setprefheight(150);
by default, gridpane
resize each control fill width of cell containing it. since list box in same column text area, has preferred width of 750, force list box have width of 750. can fix in 2 ways:
set max width of list box:
listview.setmaxwidth(260);
or, if prefer "gridpane" solution, use column constraints override default behavior:
columnconstraints leftcol = new columnconstraints(); leftcol.setfillwidth(false); grid.getcolumnconstraints().addall(leftcol, new columnconstraints());
Comments
Post a Comment