Details
Description
(Full description with images on http://stackoverflow.com/questions/16472589/layout-gridpanel-borderpanel-button-accordion)
Here is two snapshots of a SSCCE written to show a strange behavior of layout with or without accordion as a cell into a GridPane, the code to reproduce these behaviors and finally the questions I ask.
Good rendering, as expected, the "center" content of the BorderPane is a Label:
![Bad layout of the button][1]
http://i.stack.imgur.com/rtox4.png
Bad layout of the button, the "center" content of the BorderPane is an Accordion:
![Bad layout of the button][2]
http://i.stack.imgur.com/WxsXo.png
The code:
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class SSCCE extends Application {
public static final boolean TOO_BIG_BUTTON = true;
// public static final boolean TOO_BIG_BUTTON = false;
public static void main( String[] args ) {
launch( args );
}
@SuppressWarnings("nls")
@Override
public void start( Stage stage ) {
TextField tf = new TextField( "textfield" );
BorderPane bp = new BorderPane();
Label lv = new Label( "BorderPane - Center - Center - Center - Center - Center" );
lv.setPrefHeight( 96.0 );
if( TOO_BIG_BUTTON ) {
Accordion acc = new Accordion();
TitledPane tp = new TitledPane( "Titled pane", lv );
acc.getPanes().add( tp );
acc.setExpandedPane( tp );
stage.setTitle( "SSCCE - too large button" );
bp.setCenter( acc );
}
else {
stage.setTitle( "SSCCE - as expected" );
bp.setCenter( lv );
}
GridPane gp = new GridPane();
Label lbl = new Label( "Label: " );
Button btn = new Button( "button" );
btn.setMaxWidth( Double.MAX_VALUE );
gp.add( lbl, 0, 1 );
gp.add( tf , 1, 1 );
gp.add( btn, 2, 1 );
gp.add( bp , 0, 2, 3, 1 );
GridPane.setHgrow( tf , Priority.ALWAYS );
GridPane.setHgrow( btn, Priority.NEVER );
GridPane.setHalignment( btn, HPos.RIGHT );
stage.setScene( new Scene( gp ));
stage.centerOnScreen();
stage.show();
}
}
The questions:
- Why?
- How can I help the layout manager to compute the right size of the button?
[1]: http://i.stack.imgur.com/rtox4.png
[2]: http://i.stack.imgur.com/WxsXo.png
Here is two snapshots of a SSCCE written to show a strange behavior of layout with or without accordion as a cell into a GridPane, the code to reproduce these behaviors and finally the questions I ask.
Good rendering, as expected, the "center" content of the BorderPane is a Label:
![Bad layout of the button][1]
http://i.stack.imgur.com/rtox4.png
Bad layout of the button, the "center" content of the BorderPane is an Accordion:
![Bad layout of the button][2]
http://i.stack.imgur.com/WxsXo.png
The code:
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class SSCCE extends Application {
public static final boolean TOO_BIG_BUTTON = true;
// public static final boolean TOO_BIG_BUTTON = false;
public static void main( String[] args ) {
launch( args );
}
@SuppressWarnings("nls")
@Override
public void start( Stage stage ) {
TextField tf = new TextField( "textfield" );
BorderPane bp = new BorderPane();
Label lv = new Label( "BorderPane - Center - Center - Center - Center - Center" );
lv.setPrefHeight( 96.0 );
if( TOO_BIG_BUTTON ) {
Accordion acc = new Accordion();
TitledPane tp = new TitledPane( "Titled pane", lv );
acc.getPanes().add( tp );
acc.setExpandedPane( tp );
stage.setTitle( "SSCCE - too large button" );
bp.setCenter( acc );
}
else {
stage.setTitle( "SSCCE - as expected" );
bp.setCenter( lv );
}
GridPane gp = new GridPane();
Label lbl = new Label( "Label: " );
Button btn = new Button( "button" );
btn.setMaxWidth( Double.MAX_VALUE );
gp.add( lbl, 0, 1 );
gp.add( tf , 1, 1 );
gp.add( btn, 2, 1 );
gp.add( bp , 0, 2, 3, 1 );
GridPane.setHgrow( tf , Priority.ALWAYS );
GridPane.setHgrow( btn, Priority.NEVER );
GridPane.setHalignment( btn, HPos.RIGHT );
stage.setScene( new Scene( gp ));
stage.centerOnScreen();
stage.show();
}
}
The questions:
- Why?
- How can I help the layout manager to compute the right size of the button?
[1]: http://i.stack.imgur.com/rtox4.png
[2]: http://i.stack.imgur.com/WxsXo.png