I'm trying to make a window with a certain layout using 2 columns, but I can't quite get it to work the way I want.
First, some simplified example code:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class GridTest extends Application {
@Override
public void start(final Stage stage) throws Exception {
final GridPane grid = new GridPane();
grid.setHgap(5);
grid.setVgap(5);
grid.setPadding(new Insets(10, 10, 10, 10));
grid.add(new Label("Something:"), 0, 0);
final Spinner<?> s1 = new Spinner<>();
grid.add(s1, 1, 0);
grid.add(new Label("Another thing:"), 0, 1);
final Spinner<?> s2 = new Spinner<>();
grid.add(s2, 1, 1);
final Button b = new Button("A button");
grid.add(b, 0, 2, 2, 1);
stage.setScene(new Scene(grid, 400, 150));
stage.show();
}
public static void main(final String... args) {
launch(args);
}
}
Here is what I want:
- the spinners and the button should use all available width
- the labels should always keep their preferred width, and never change their size
- when I make the window wider, the spinners and the button should grow
- when I make the window narrower, the spinners and the button should shrink, but not below their preferred width
I tried using all sorts of constraints, but I couldn't get everything to work right. I think the main problem I found was that after getting the spinners to use the available width, they refused to shrink when narrowing the window.
I'm using Oracle JDK 1.8.0_60 in Linux.