Tamanho do ComboBox no GridPane - JavaFX

2 respostas
java
J

Olá pessoal

Estou desenvolvendo um tela em JavaFX. Estou usando o layout GridPane.
Ao adicionar um ComboBox na GridPane, o ComboBox não fica com o tamanho da célula reservada para ele, pois o seu tamanho depende do tamanho dos valores contido nesse ComboBox.

Alguém sabe se há alguma configuração via código java ou até mesmo css para fazer com que o ComboBox ocupe todo o espaço reservado para ele?

2 Respostas

A

Olá, se estiver usando FXML, coloque o seu combo dentro de um AnchorPane e coloque as constantes de layout como zero:
<AnchorPane GridPane.hgrow="SOMETIMES" GridPane.rowIndex="1" GridPane.vgrow="SOMETIMES"> <children> <ComboBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> </children> </AnchorPane>

<strong>código completo:</strong>

`    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>

<?import javafx.scene.control.ChoiceBox?>

<?import javafx.scene.control.ComboBox?>

<?import javafx.scene.layout.AnchorPane?>

<?import javafx.scene.layout.ColumnConstraints?>

<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-  Infinity" prefHeight="80.0" prefWidth="345.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60">
 <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="161.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="164.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="127.0" minHeight="0.0" prefHeight="0.0" />
<RowConstraints maxHeight="-Infinity" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="-Infinity" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
  <AnchorPane GridPane.hgrow="SOMETIMES" GridPane.rowIndex="1" GridPane.vgrow="SOMETIMES">
     <children>
        <ComboBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
     </children>
  </AnchorPane>
  <AnchorPane GridPane.columnIndex="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="2" GridPane.vgrow="SOMETIMES">
     <children>
        <ChoiceBox prefWidth="150.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
     </children>
  </AnchorPane>
</children>
<padding>
  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</GridPane>

`

L

Basta setar as propriedades do gridPane onde esta seu combobox para

Coluna Vertical Propriedade:
Min Width: USE_COMPUTED_SIZE
Pref Width: USE_COMPUTED_SIZE
Max Width: MAX_VALUE
hGrow: INHERIT

Linha Horizontal Propriedade:
Min Width: USE_COMPUTED_SIZE
Pref Width: USE_COMPUTED_SIZE
Max Width: MAX_VALUE
vGrow: INHERIT

se fizer isto via Fxml no editor vai funcionar;

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?>

“<“GridPane maxHeight=”-Infinity” maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight=“400.0” prefWidth=“600.0” xmlns:fx=“http://javafx.com/fxml/1” xmlns=“http://javafx.com/javafx/8”>
"<“columnConstraints>
”<“ColumnConstraints minWidth=“10.0” prefWidth=“100.0” />
”<“ColumnConstraints maxWidth=“1.7976931348623157E308” />
”<"/columnConstraints>
"<“rowConstraints>
”<“RowConstraints maxHeight=“1.7976931348623157E308” />
”<“RowConstraints maxHeight=“1.7976931348623157E308” />
”<“RowConstraints maxHeight=“1.7976931348623157E308” />
”<"/rowConstraints>
"<“children>
”<“ComboBox prefWidth=“150.0” GridPane.columnIndex=“1” GridPane.rowIndex=“2” />
”<"/children>
"<"/GridPane>

Desculpe as “<” tive que colocar não sei porque mas estou sem tempo.

abraços.

Criado 29 de novembro de 2016
Ultima resposta 30 de nov. de 2016
Respostas 2
Participantes 3