如何在Java FX中创建复选框的可滚动“下拉”?

时间:2019-01-21 20:20:12

标签: java user-interface javafx scrollbar

我是JavaFX的新手,我正在尝试创建检查项目的“下拉列表”。我正在尝试使下拉菜单滚动。我可以使用ComboBox轻松地做到这一点(使用setVisibleowCount(int)),但是ComboBox仅允许在关闭对话框之前选择一项,并且似乎不是要使用的正确对象。

我当前正在使用带有CheckMenuItems的菜单按钮。 ListView似乎很有用,但是我不太确定如何集成它。如果有人可以帮助,那就太好了。谢谢。

Current Status

1 个答案:

答案 0 :(得分:0)

由于您无法使用CheckComboBox,所以我会查看手风琴+ TitledPane是否适合您的工作。
这是一个示例:

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception{
        VBox root = new VBox();
        root.getChildren().add(new Label("Select Number of Checkboxes you feel like clicking"));

        VBox vBox = new VBox();
        for (int i = 0; i < 5; i++)
            vBox.getChildren().add(new CheckBox("i:" + i));

        ScrollPane scrollPane = new ScrollPane(vBox);
        //Easily changeable Max Height
        scrollPane.setMaxHeight(10);

        // Create TitledPane.
        TitledPane titledPane = new TitledPane("Check Boxes", scrollPane);
        //Add to Accordion
        Accordion accordion = new Accordion(titledPane);
        //Add to root VBox
        root.getChildren().add(accordion);

        root.getChildren().add(new Label("Some Other Content"));

        stage  = new Stage();
        stage.setHeight(200);
        stage.setScene(new Scene(root));
        stage.setAlwaysOnTop(true);
        stage.show();
    }

    public static void main(String[] args) { launch(args); }

}
相关问题