When using GirdPanes in JavaFX 2, I come across a slightly strange layout. Although it usually seems that the distribution of spatial spaces is even between several fields, in some other cases this is not so. Here is an example showing the effect:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class TestDialog extends Stage {
public TestDialog() {
super();
setTitle("Test");
GridPane grid = new GridPane();
grid.setHgap(5);
grid.setVgap(5);
grid.setPadding(new Insets(10, 10, 10, 10));
final TextField tField1 = new TextField();
final TextField tField2 = new TextField();
tField2.setMaxWidth(200);
grid.add(createLabel("Field 1:"), 0, 0);
grid.add(tField1, 1, 0);
grid.add(createLabel("Field 2:"), 2, 0);
grid.add(tField2, 3, 0);
Pane pane = new Pane();
pane.setMinSize(100, 100);
pane.setStyle("-fx-background-color: black");
grid.add(pane, 0, 1, 4, 1);
grid.setMinSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
setScene(new Scene(grid));
}
private Label createLabel(String text) {
Label label = new Label(text);
label.setMinSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
return label;
}
public static void main(String[] args) {
TestApplication.main(args);
}
public static class TestApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
TestDialog dialog = new TestDialog();
dialog.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}
}
In this example, the dialog will change beautifully at startup as is. If the minimum size of the additional panel is changed to the version in the comment, then everything starts to go wrong: the additional space in the first line will be turned into empty space (instead of increasing the first field), reducing the size of the dialog box is far from the already small first field, leaving an empty area intact.
GridPane ? , , .
JavaFX JDK 1.7.0_21.