java - What is the preferred size of a control? -


my first attempt @ playing javafx , i'm trying understand little bit layout management. how access preferred size of control?

in example below attempting set maximum width 200 pixels greater preferred width. is, want allow button grow (up maximum) width of frame increased.

however when run code preferred width -1, adding 200 preferred width gives maximum width of 199.

import javafx.application.application; import javafx.event.*; import javafx.stage.stage; import javafx.scene.parent; import javafx.scene.scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.geometry.insets;  public class borderpanesscce extends application {     @override     public void start(stage primarystage)     {         button button = new button( "button @ preferredsize" );         button.setmaxwidth( button.getprefwidth() + 200 );         system.out.println(button.prefwidth(-1) + " : " + button.getprefwidth());          button.setonaction(new eventhandler<actionevent>()         {             @override             public void handle(actionevent event)             {                 system.out.println("width: " + button.getwidth());             }         });          hbox root = new hbox();         hbox.sethgrow(button, priority.always);         root.getchildren().add(button);          scene scene = new scene(root);         primarystage.settitle("java fx");         primarystage.setscene(scene);         primarystage.show();         system.out.println(button.prefwidth(-1) + " : " + button.getprefwidth());     }      public static void main(string[] args)     {         launch(args);     } } 

when run code , click on button see: width: 139.0

after resize width of frame button large possible , click on button see: width: 199.0

i hoping see width: 339.0 (ie. 139 + 200)

so, how access preferred size/width of control?

getprefwidth() returns use_computed_size flag (which -1) default.

you can use prefwidth(-1) internally computed preferred width. preferred width not calculated until node being layed out layout pane (hbox in example). happens first time when stage shown.

you have multiple options, if want maximum width depend on preferred width. 1 set preferred width fixed value setprefwidth() prior setting maximum width.

another implement custom layout algorithm - either on node or on layout pane. here example using custom button.

// button maxwidth prefwidth + 200 button button = new button() {     @override     protected double computemaxwidth(double height)     {         return this.prefwidth(height) + 200.0;     } }; 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -