简体   繁体   English

JavaFX:GridPane中的ComboBox导致不必要的调整大小

[英]JavaFX: ComboBox in GridPane causes unnecessary resizing

I have a GridPane with 5 columns. 我有5列的GridPane These are my hgrow setting for the column constraints respectively. 这些分别是我对列约束的hgrow设置。

  1. SOMETIMES
  2. ALWAYS
  3. SOMETIMES (fixed sized) SOMETIMES (固定尺寸)
  4. SOMETIMES
  5. ALWAYS

The whole GridPane is basically a layout for a two-column form. 整个GridPane基本上是两列形式的布局。 My columns 1 and 4 (index 0 and 3) are field labels, while columns 2 and 5 are the input fields ( TextField etc). 我的第1列和第4列(索引0和3)是字段标签,而第2列和第5列是输入字段( TextField等)。 Column 3 is just a fixed-width padding between the two form columns. 第3列只是两个表单列之间的固定宽度填充。

I have some ComboBox in my form - some are on the left column, some on the right column. 我的表单中有一些ComboBox一些在左列,一些在右列。 I have set the max width of all the ComboBox to MAX_VALUE . 我已将所有ComboBox的最大宽度设置为MAX_VALUE

When the form loads, the layout looks like what I had intended (both input-field columns have equal width). 加载表单时,布局看起来像我的预期(两个输入字段列的宽度均相等)。 However, when I click on any of the ComboBox , it would cause the column that the clicked ComboBox is on to increase in width - which causes the other input-field column to decrease by the same amount. 但是,当我单击任何ComboBox ,这将导致单击ComboBox所在的列的宽度增加-这将导致其他输入字段列减少相同的数量。

I can alternate clicking on the ComboBox on the left and right, and the widths of both columns would keep changing. 我可以交替单击左侧和右侧的ComboBox ,两列的宽度都会不断变化。 The change would get smaller and smaller and eventually the columns would stop resizing. 变化将变得越来越小,最终列将停止调整大小。 The end result is that the two columns are not equal in width. 最终结果是两列的宽度不相等。

I have the same problem for another control JFXDatePicker from JFoenix library. 对于来自JFoenix库的另一个控件JFXDatePicker ,我也有同样的问题。

Another constraint I have is that this form is within a SplitPane , so I have to avoid setting explicit widths. 我的另一个约束是此表单位于SplitPane ,因此我必须避免设置显式宽度。

What can I do to fix this problem? 我该怎么做才能解决此问题?

Edit 编辑

I guess I will attach a sample here considering how little view this question has. 考虑到这个问题的观点很少,我想在这里附上一个样本。

@Override
public void start(Stage primaryStage) throws Exception {
    VBox root = FXMLLoader.load(getClass().getResource("Test.fxml"));
    Scene sc = new Scene(root);
    primaryStage.setScene(sc);
    primaryStage.show();
}

FXML: FXML:

<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
    </padding>
   <children>
      <GridPane hgap="6.0" vgap="6.0" VBox.vgrow="ALWAYS">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" />
          <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" />
            <ColumnConstraints hgrow="SOMETIMES" />
            <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Left 1" GridPane.halignment="RIGHT" />
            <Label text="Left 2" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
            <Label text="Left 3" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
            <Label text="Right 1" GridPane.columnIndex="2" GridPane.halignment="RIGHT" />
            <Label text="Right 2" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
            <Label text="Right 3" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="1" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="3" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="3" GridPane.rowIndex="2" />
         </children>
      </GridPane>
   </children>
</VBox>

The result: 结果:

结果

As shown in the GIF, the layout is what I wanted when the window is initially launched. 如GIF所示,布局是我最初启动窗口时想要的布局。 When I clicked on one of the combobox at the right side, the column suddenly increased its width and shrunk the other column. 当我单击右侧的组合框时,该列的宽度突然增大,而另一列缩小了。 I need the width of both columns to remain constant when any of the comboboxes are clicked. 单击任何组合框时,我都需要两列的宽度保持不变。

Update 更新资料

It seems like I had missed out some information in my question. 好像我错过了我的问题中的一些信息。 The final result I need is a GridPane with 4 columns in two label-input pairs. 我需要的最终结果是在两个标签输入对中有4列的GridPane An extra column can be added between the pairs to add additional padding between the pairs, but it's optional. 可以在两对之间添加一个额外的列,以在两对之间添加其他填充,但这是可选的。 The whole grid will be in a resizable environment (for example in my example - a resizable Stage). 整个网格将处于可调整大小的环境中(例如,在我的示例中为可调整大小的舞台)。 I believe resizable environment is not unreasonable, otherwise I would have given all columns absolute sizes (or even use any Pane and gave it absolute position) and the problem wouldn't have existed. 我相信可调整大小的环境并非没有道理,否则我将为所有列提供绝对大小(甚至使用任何Pane并为其提供绝对位置),并且该问题将不存在。

Both the labels must always be visible (unless the whole GridPane is reduced to a terribly small size), and any additional space must be given to the input controls ( TextField , ComboBox etc). 两个标签都必须始终可见(除非将整个GridPane缩小到非常小的尺寸),并且必须为输入控件( TextFieldComboBox等)提供任何额外的空间。 Similarly, if the GridPane is shrunk, I expect the input columns to shrink instead of the label columns. 同样,如果GridPane缩小,我希望输入列会缩小,而不是标签列。 The label columns should maintain a fixed size throughout, and this size is the minimum size that is needed to display all labels without ellipsis appearing. 标签列始终应保持固定大小,并且此大小是显示所有标签而不出现省略号所需的最小大小。

As the final form has a mixture of different input controls, it would make the ComboBox look weird if it is not resized to the same width as other input controls in the same column. 由于最终形式混合了不同的输入控件,因此如果未将ComboBox大小调整为与同一列中的其他输入控件相同的宽度,将使ComboBox看起来很奇怪。 I admit that this requirement is me being picky. 我承认这个要求使我很挑剔。

I also expect both the input columns to have the same width, and that this must remain true at all times. 我还希望两个输入列都具有相同的宽度,并且该宽度必须始终保持不变。

Another Update 另一个更新

I have made a bug report on this, and it is currently still open with a P3 priority. 我对此进行了一个错误报告 ,并且目前仍以P3优先级打开。

I just played with the setting to see what I could come up with. 我只是玩了一下设置,看我能想到什么。 This appears to be working. 这似乎正在工作。 I used percentages on the columns. 我在列上使用了百分比。

<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="162.0" prefWidth="326.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <!--Insets bottom="30.0" left="30.0" right="30.0" top="30.0" /-->
    </padding>
   <children>
      <GridPane hgap="6.0" vgap="6.0" VBox.vgrow="SOMETIMES">
        <columnConstraints>
          <ColumnConstraints halignment="RIGHT" hgrow="NEVER" percentWidth="15.0" />
          <ColumnConstraints hgrow="SOMETIMES" percentWidth="35.0" />
            <ColumnConstraints halignment="RIGHT" hgrow="NEVER" percentWidth="15.0" />
            <ColumnConstraints hgrow="SOMETIMES" percentWidth="35.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Left 1" GridPane.halignment="RIGHT" />
            <Label text="Left 2" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
            <Label text="Left 3" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
            <Label text="Right 1" GridPane.columnIndex="2" GridPane.halignment="RIGHT" />
            <Label text="Right 2" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
            <Label text="Right 3" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="1" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="3" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="3" GridPane.rowIndex="2" />
         </children>
      </GridPane>
   </children>
</VBox>

This is a pretty old post, but I just ran into this issue and found that the best way to do it is to dynamically add width handlers to both the ComboBox and the GridPane, I have a 4 x 2 grid with label/combo/label/combo much like you do: 这是一篇很老的文章,但是我刚遇到这个问题,发现最好的方法是将宽度处理程序动态添加到ComboBox和GridPane中,我有一个4 x 2的带有label / combo / label的网格/ combo就像您一样:

    cmbOne.setOnShowing(e -> detailsGrid.getColumnConstraints().get(1).setMaxWidth(cmbOne.getWidth()));
    cmbTwo.setOnShowing(e -> detailsGrid.getColumnConstraints().get(3).setMaxWidth(cmbTwo.getWidth()));
    detailsGrid.widthProperty().addListener(e -> {
        detailsGrid.getColumnConstraints().get(1).setMaxWidth(Double.MAX_VALUE);
        detailsGrid.getColumnConstraints().get(3).setMaxWidth(Double.MAX_VALUE);
    });

I setup my two combo boxes to lock down the ColumnConstraint prior to opening, and then when the grid width is changed (by a resize) I allow it to increase size again. 我设置了两个组合框以在打开之前锁定ColumnConstraint,然后在更改网格宽度(通过调整大小)后,我允许它再次增加大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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