Sarah Szabo
Sarah Szabo

Reputation: 10815

JavaFX 2.0: Addition of Text Shifts Other Controls

When this class runs, the Text that is generated on the bottom (After the button is pressed) is pushing the other controls away from each other and compressing them. Is there any way that I can keep the Text but not have the controls above it compressed? NOTE: This class can be run by calling Application.launch(CSSTrial1.class); from outside the class.

public class CSSTrial1 extends Application {

@Override
public void start(Stage stage) throws Exception {
    stage.setTitle("CSS Trial");
    stage.setResizable(false);
    final GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25));
    grid.add(new Label("Character Name:"), 0, 0);
    final TextField nameField = new TextField("");
    nameField.setPromptText("Randel James");
    grid.add(nameField, 1, 0);
    grid.add(new Label("Weapon:"), 0, 1);
    final ComboBox<String> weapons = new ComboBox<>();
    weapons.getItems().addAll("Katsuragi x2", "Assault Rifle",
            "Pistol", "RPG-7", "Barret 50. Cal");
    grid.add(weapons, 1, 1);
    HBox box = new HBox(10);
    box.setAlignment(Pos.BOTTOM_RIGHT);
    Button submit = new Button("Submit Character");
    submit.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            grid.add(new Text("Signed In As: " + nameField.getText()
                    + ", Weapon: " + weapons.getValue()), 0, 6);
        }
    });
    box.getChildren().add(submit);
    grid.add(box, 1, 3);
    Scene scene = new Scene(grid, 300, 275);
    stage.setScene(scene);
    stage.show();
}

}    

Upvotes: 2

Views: 177

Answers (3)

James_D
James_D

Reputation: 209330

Define a ColumnConstraints object, and use it in the grid pane:

ColumnConstraints firstColumn = new ColumnConstraints();
grid.getColumnConstraints().addAll(firstColumn);

You can set the min, pref, and max width on the column constraints to control how much that column is allowed to be resized (for example, setting all three to the same fixed value will force the column to be that size).

You might also want to set the wrapping width of your text, so that it takes up less space.

Upvotes: 1

dajood
dajood

Reputation: 4050

Maybe try to insert the text with another column span such as in this altered code:

 submit.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            grid.add(new Text("Signed In As: " + nameField.getText()
                    + ", Weapon: " + weapons.getValue()), 0, 6,2,1);//colum span=2, but row span is1
        }
    });

Upvotes: 0

Kent Shikama
Kent Shikama

Reputation: 4060

According to the Javadocs:

By default, rows and columns will be sized to fit their content; a column will be wide enough to accommodate the widest child, a row tall enough to fit the tallest child.

Since you are adding the nameField to the first column, when the length of that text expands, the whole columns expands in length, causing the "pushing of the controls away from each other." I would suggest either creating a new Pane, have the nameField span two columns, or using a different layout. You may even want to consider using FXML with the JavaFX Scene Builder.

Upvotes: 0

Related Questions