Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to show ListView with ListCell in Popup?

Emon HaqueDec 8 2021 — edited Dec 8 2021

Here's the ListView in the primary stage:
1.PNGeverything is displayed correctly. Here's my ListCell<Preson>:

public class PersonTemplate extends ListCell<Person> {
    @Override
    protected void updateItem(Person item, boolean empty) {
        super.updateItem(item, empty);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        if(item == null || empty) setGraphic(null);
        else{
            setPrefWidth(0);
            var spacer = new Region();
            HBox.setHgrow(spacer, Priority.ALWAYS);
            setGraphic(new HBox(new Text(item.name), spacer, new Text(item.phone)));
        }
    }
}

In a custom control named SelectionBox I tried to use a ListView with PersonTemplate like this:

public class SelectionBox<T> extends GridPane {
    Popup popup;
    TextField input;
    ListView<T> listView;
    Label hintLabel;
    Separator line;
    ActionButton close, open;
    SVGPath svgIcon;
    public SelectionBox(String hint, String icon, ObservableList<T> list, ListCell<T> template){
        listView = new ListView<>(list);
        listView.setCellFactory(v -> template);
        listView.setMaxHeight(200);
        popup = new Popup();
        popup.getContent().add(listView);
        popup.setAutoHide(true);
        popup.setHideOnEscape(true);
        input = new TextField();
        input.setBackground(null);
        hintLabel = new Label(hint);
        line = new Separator();
        line.setBorder(new Border(new BorderStroke(Color.LIGHTBLUE, Color.TRANSPARENT, Color.TRANSPARENT,Color.TRANSPARENT, BorderStrokeStyle.SOLID, BorderStrokeStyle.NONE,BorderStrokeStyle.NONE,BorderStrokeStyle.NONE, null, null, null)));
        open = new ActionButton(Icons.DownArrow, 24, "show");
        close = new ActionButton(Icons.CloseCircle, 16, "remove");
        svgIcon = new SVGPath();
        svgIcon.setContent(icon);
        addRow(0, svgIcon, input, open);
        addRow(1,line);
        setColumnSpan(line, 3);
        setHgrow(input, Priority.ALWAYS);
        input.textProperty().addListener(this::onTextChanged);
    }
    void onTextChanged(ObservableValue<? extends String> observable, String oldValue, String newValue){
        if(oldValue.equals(newValue)) return;
        if(!popup.isShowing()){
            var point = input.localToScreen(0.0, 0.0);
            listView.setMinWidth(input.getWidth());
            popup.show(input, point.getX(), point.getY() + getHeight());
        }
    }
}

if I comment listView.setCellFactory(v -> template) in the constructor it looks like this:
2.gifand if I uncomment that line, I don't see any ListCell inside the ListView . It looks like this with that line uncommented:
3.gifHow to show it properly in the Popup?

Comments
Post Details
Added on Dec 8 2021
0 comments
482 views