首页 文章

Gluon Mobile Scrollable下拉按钮

提问于
浏览
1

有没有办法让下拉按钮内容可滚动?我有很多菜单项将下拉列表扩展到设备屏幕之外 .

try {
        DropdownButton merchantChooser = (DropdownButton) this.view.lookup("#review-merchant-chooser");
        VBox container = (VBox) this.view.lookup("#review-container");
        TextArea area = (TextArea) this.view.lookup("#review-text");
        StackPane sp = (StackPane) this.view.lookup("#review-wrapper");
        Button btn = (Button) this.view.lookup("#review-submit");

        Utility.setBackground(sp, Utility.bannerImg2);
        Utility.setFadedBackground(true, container);

        merchantChooser.getItems().clear();
        if (MainView.merchants.isInitialized()) {
            btn.setDisable(false);
            MenuItem firstItem = null;

            for (int i = 0; i < MainView.merchants.size(); i++) {
                Label label = new Label(MainView.merchants.get(i).getName());
                label.setWrapText(true);
                MenuItem item = new MenuItem(label.getText());

                merchantChooser.getItems().add(item);

                if(i == 0)
                    firstItem = item;
            }

            merchantChooser.setSelectedItem(firstItem);
            merchantChooser.setPrefWidth(200);
        }
        else
        {
            btn.setDisable(true);
        }

        btn.setOnMouseClicked(e -> {
            //ENTER BACKEND POST HERE TO SEND REVIEW TO DATABASE!!
        });

    } catch (NullPointerException nex) {
        System.out.println("Null pointer at AddReviewView");
    } catch (Exception ex) {
        System.out.println("Other exception in discount view");
        System.out.println(ex.getMessage());
    }

1 回答

  • 2

    如果你有很多项目,那么 DropdownButton 可能不是这项工作的最佳控制 . 它没有提供使其可滚动的方法 .

    您可以查看其他选项,例如 PopupView control .

    此控件允许自定义内容,因此您可以添加 ScrollPane ,其中包含所有项目的 VBox . 而不是 MenuItem 控件,您可以使用常规 Button .

    这是一个快速实现,但它也被设计为 DropdownButton .

    public BasicView(String name) {
        super(name);
    
        Button button = new Button("Click me", new Icon(MaterialDesignIcon.ARROW_DROP_DOWN));
        button.getStyleClass().add("flat");
        button.setStyle("-fx-border-color: lightgray; -fx-border-width: 0 0 1 0");
        button.setContentDisplay(ContentDisplay.RIGHT);
    
        PopupView popup = new PopupView(button);
    
        VBox vBox = new VBox();
        for (int i = 0; i < 100; i++) {
            Button item = new Button("item " + i);
            item.setPrefWidth(100);
            item.getStyleClass().add("flat");
            item.setOnAction(e -> {
                System.out.println("item " + item.getText());
                popup.hide();
            });
            vBox.getChildren().add(item);
        }
        ScrollPane scrollPane = new ScrollPane(vBox);
        scrollPane.setMaxHeight(200);
        scrollPane.setPrefWidth(110);
    
        popup.setContent(scrollPane);
    
        button.setOnAction(event -> popup.show());
    
        setCenter(button);
    }
    

相关问题