A.Sharma
A.Sharma

Reputation: 2799

Gluon Mobile Scrollable Dropdown Button

Is there a way to make the dropdown button contents scrollable? I have a lot of menu items that extend the dropdown list beyond the screen of the device.

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());
    }

Upvotes: 1

Views: 321

Answers (1)

Jos&#233; Pereda
Jos&#233; Pereda

Reputation: 45456

If you have many items, maybe the DropdownButton is not the best control for the job. It doesn't provide a way to make it scrollable.

You can have a look at other options, like the PopupView control.

This control allows custom content, so you can add a ScrollPane with a VBox that will contain all the items. Instead of MenuItem controls, you can use regular Button ones.

This is a quick implementation, but it is also styled as the 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);
}

Upvotes: 3

Related Questions