简体   繁体   中英

HBox doesn't fill the available space in center of borderpane

I placed an HBox into the center of a BorderPane , that is the only direct Node on a DialogPane of my Dialog by using this code:

    public GameDialog(Player[] players) {
          setTitle("Spiel eintragen");
          setWidth(WIDTH);
          setHeight(HEIGHT);

          createLeftBox();
          createRightBox(players);

          // Center
          this.center = new HBox(leftBox, rightBox);
          center.setStyle("-fx-border-color: #00ff00;");
          center.getChildren()
                .forEach(b -> HBox.setMargin(b, INSETS));

          // Bottom
          this.btnOk = new Button("ok");
          this.btnCancel = new Button("abbrechen");
          this.bottom = new HBox(btnOk, btnCancel);
          bottom.getChildren()
                .forEach(b -> HBox.setMargin(b, INSETS));

          // Pane
          this.pane = new BorderPane(center);
          pane.setBottom(bottom);
          pane.setPrefSize(WIDTH, HEIGHT);
          getDialogPane().setPrefSize(WIDTH, HEIGHT);
          getDialogPane().getChildren().add(pane);
          ...
    }

    private void createLeftBox() {
      // Points spinner
      List<Integer> points = Stream.iterate(0, Util::increment)
                                   .limit(MAX_POINTS)
                                   .collect(Collectors.toList());
      this.spPoints = new Spinner<>(observableArrayList(points));

      // Solo combobox
      List<Solotype> soli = Arrays.asList(Solotype.values());
      this.cbSolo = new ComboBox<>(observableArrayList(soli));
      cbSolo.setOnAction(e -> changed());

      // Bock checkbox
      this.chBock = new CheckBox("Bock");

      // Left box
      leftBox = new VBox(spPoints, cbSolo, chBock);
      leftBox.getChildren()
             .forEach(n -> VBox.setMargin(n, INSETS));

      leftBox.setStyle("-fx-border-color: #ff0000");
   }

   private void createRightBox(Player[] players) {
      List<Player> playerlist = Arrays.asList(players);
      // Right box
      rightBox = new VBox();
      List<Node> rightChildren = rightBox.getChildren();
      this.playerBoxes = playerlist.stream()
                                   .collect(toMap(p -> p,
                                            p -> new HBox(4)));
      this.players = playerlist.stream()
                               .collect(toMap(p -> p,
                                              this::createToggleGroup));
      playerBoxes.values()
                 .stream()
                 .peek(rightChildren::add)
                 .forEach(b -> VBox.setMargin(b, INSETS));

      rightBox.setStyle("-fx-border-color: #ff0000");
   }

My class GameDialog extends the javafx-class Dialog<R> . However the HBox in the center of the BorderPane doesn't fill all of the available space. It just looks like this:

对话框的屏幕截图

I tried to setMinWidth, setMinHeight, setPrefSize, setFillWidth(true) and setFillHeight(true) on several of the H- and VBoxes. Nothing Changed. I just don't understand this behaviour. Any ideas?

I couldn't fully recreate your example (since some code is missing, I used TextField instead of those toggle groups), but had some success with this solution.

Instead of using:

getDialogPane().getChildren().add(pane);

use:

getDialogPane().setContent(pane);

And the controls should use all the available space.


在此处输入图片说明

(I also removed the setting of the preferred width and height, so the image was a little smaller. Setting the preferred size of the pane doesn't change anything any ways.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM