【问题标题】:How to set context menu width to match choice box如何设置上下文菜单宽度以匹配选择框
【发布时间】:2017-05-20 14:50:54
【问题描述】:

我使用场景构建器 2.0 创建了 ChoiceBox 并附加到 IntelliJ Idea 并添加以下代码以将项目添加到 Main 类中的 ChoiceBox

ChoiceBox genderBox = (ChoiceBox) scene.lookup("#gender");
genderBox.setItems(FXCollections.observableArrayList("Boy", "Girl"));
genderBox.getSelectionModel().select(0); 

FXML 的选择框代码如下

<ChoiceBox id="gender" prefHeight="16.0" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="11">
           <GridPane.margin>
              <Insets left="10.0" />
           </GridPane.margin>
</ChoiceBox>

无论如何,在运行 Main 类时,我看到如下选择框。选择框宽度与上下文菜单(弹出)宽度不匹配。

如何设置上下文菜单的宽度?

编辑

无论如何,在组合框中为上下文菜单设置宽度都可以正常工作。所以我用组合框移动。 see example fxml.

【问题讨论】:

    标签: javafx java-8 scenebuilder dropdownchoice


    【解决方案1】:

    首先,设置 ChoiceBox 本身的宽度...

    <ChoiceBox prefWidth="150">
        ...
    </ChoiceBox>
    

    然后,通过 CSS 设置弹出列表的宽度...

    .choice-box .menu-item > .label {
        -fx-pref-width: 100;
    }
    

    注意:由于下拉按钮,该框比列表宽约 50 像素。

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 2018-11-28
      • 2013-06-22
      • 1970-01-01
      • 2018-10-06
      相关资源
      最近更新 更多