java - Button alignment in JavaFX -


i created javafx dialog close button:

final int xsize = 300; final int ysize = 280; final color backgroundcolor = color.white; final string text = "sql browser version 1.0";  final stage aboutdialog = new stage(); aboutdialog.initmodality(modality.window_modal);  button closebutton = new button("close"); closebutton.setalignment(pos.bottom_center);  closebutton.setonaction(new eventhandler<actionevent>() {     @override     public void handle(actionevent arg0) {         aboutdialog.close();     } });  scene aboutdialogscene = new scene(vboxbuilder.create()     .children(new text(text), closebutton)     .alignment(pos.center)     .padding(new insets(10))     .build(), xsize, ysize, backgroundcolor);  aboutdialog.setscene(aboutdialogscene); aboutdialog.show(); 

i want display button @ bottom of dialog. used set alignment: closebutton.setalignment(pos.bottom_center); reason button displayed @ center of dialog. can tell me how can fix this?

if want use vbox this, method looking is:

vbox.setvgrow(node, priority.always);


by default vbox place children 1 under other top left of place it. children don't expand fill of available vertical area, unless set vgrow constraint on child unbounded max height.

a few different ways can layout seek (there others well):

  1. use stackpane instead of vbox , align button stackpane.setalignment(closebutton, pos.bottom_center);
  2. use anchorpane instead of vbox , set constraints on anchorpane appropriately.
  3. use spring region empty region expands fill empty space.

sample spring region:

region topspring    = new region(); region bottomspring = new region(); scene aboutdialogscene = new scene(vboxbuilder.create()         .children(topspring, new text(text), bottomspring, closebutton)         .alignment(pos.center)         .padding(new insets(10))         .build(), xsize, ysize, backgroundcolor); vbox.setvgrow(topspring, priority.always); vbox.setvgrow(bottomspring, priority.always); 

calling closebutton.setalignment(pos.bottom_center); sets alignment of things (text , graphic) within closebutton, not alignment of closebutton within it's parent (which want).


for understanding how layout constraints work, scenebuilder tool play around , scenicview can debug layout issues in existing code.


here few fxml samples of layout can load scenebuilder see how different layout options work.

all of samples below can written in plain java using javafx api if prefer. wrote them in fxml makes layouts easy preview in scenebuilder.

fxml sample using stackpane:

<?xml version="1.0" encoding="utf-8"?>  <?import java.lang.*?> <?import java.util.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?>  <stackpane id="stackpane" maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="280.0" prefwidth="300.0" xmlns:fx="http://javafx.com/fxml">   <children>     <label text="sql browser version 1.0" />     <button mnemonicparsing="false" text="button" stackpane.alignment="bottom_center" />   </children>   <padding>     <insets bottom="10.0" left="10.0" right="10.0" top="10.0" />   </padding> </stackpane> 

snapshot of fxml

and same thing spring regions:

<?xml version="1.0" encoding="utf-8"?>  <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?>  <vbox alignment="center" prefheight="280.0" prefwidth="300.0" xmlns:fx="http://javafx.com/fxml">   <children>     <region prefheight="-1.0" prefwidth="-1.0" vbox.vgrow="always" />     <label text="sql browser version 1.0" />     <region prefheight="-1.0" prefwidth="-1.0" vbox.vgrow="always" />     <button mnemonicparsing="false" text="close" />   </children> </vbox> 

and same thing label set expand fill empty space in vbox:

<?xml version="1.0" encoding="utf-8"?>  <?import java.lang.*?> <?import java.util.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?>  <vbox alignment="center" prefheight="280.0" prefwidth="300.0" xmlns:fx="http://javafx.com/fxml">   <children>     <label maxheight="1.7976931348623157e308" text="sql browser version 1.0" vbox.vgrow="always" />     <button mnemonicparsing="false" text="close" />   </children>   <padding>     <insets bottom="10.0" left="10.0" right="10.0" top="10.0" />   </padding> </vbox> 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -