Reputation: 33
I´m new to Java.
I want to fit a TextArea
in a GridPane
. I tried this for the last few hours with this result:
As you see, the TextArea
is much bigger than my Gridpane
. Here is my code:
GridPane root = new GridPane();
root.setGridLinesVisible(true);
root.setHgap(20);
root.setVgap(8);
root.setPadding(new Insets( 10, 10, 10, 10));
ColumnConstraints col1 = new ColumnConstraints();
col1.setPercentWidth(50);
ColumnConstraints col2 = new ColumnConstraints();
col2.setPercentWidth(50);
root.getColumnConstraints().addAll(col1,col2);
RowConstraints row1 = new RowConstraints();
RowConstraints row2 = new RowConstraints();
RowConstraints row3 = new RowConstraints();
RowConstraints row4 = new RowConstraints();
row1.setPrefHeight(20);
row2.setPrefHeight(20);
row3.setPrefHeight(40);
row4.setPrefHeight(20);
root.getRowConstraints().addAll(row1,row2,row3,row4);
GridPane.setVgrow(root, Priority.ALWAYS);
Label Opt1 = new Label("Operand 1");
Label Opt2 = new Label("Operand 2");
Label Erg = new Label("Ergebnis");
TextArea txtOpt1 = new TextArea();
TextArea txtOpt2 = new TextArea();
TextArea txtErg = new TextArea();
txtErg.setEditable(false);
Button btnPlus = new Button("+");
Button btnMinus = new Button("-");
Button btnMal = new Button("*");
Button btnGeteilt = new Button("/");
HBox Opts = new HBox(10);
HBox hblbl1 = new HBox();
hblbl1.getChildren().add(txtOpt1);
Opts.getChildren().add(btnGeteilt);
Opts.getChildren().add(btnMal);
Opts.getChildren().add(btnMinus);
Opts.getChildren().add(btnPlus);
Opts.setAlignment(Pos.CENTER);
root.add(Opt1, 0, 0);
root.add(Opt2, 0, 1);
root.add(hblbl1, 1, 0);
root.add(txtOpt2, 1, 1);
root.add(Opts, 0, 2, 2, 1);
root.add(Erg, 0, 3);
root.add(txtErg, 1, 3);
Upvotes: 0
Views: 1075
Reputation: 13427
the TextArea is much bigger than my Gridpane
Because you are restricting the rows of the grid pane to a height which is smaller than what the text area can fit in.
It looks like what you are looking for is actually TextField
, not TextArea
. I would remove the constraints on the rows and use TextFields
instead:
Also, GridPane.setVgrow(root, Priority.ALWAYS);
does nothing. setVgrow
specifies which component within the grid pane should grow and how. If you specify the grid pane itself as the component it's meaningless.
Upvotes: 1