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!

How to properly customize the text label of a JavaFX ComboBox

2689089Jun 8 2014 — edited Jun 10 2014

Hi,

switching from Swing to JFX 8, I am trying to find out how to do something I would normally do in a super-simple ListCellRenderer, i.e. customize the String displayed for an item. I tried this:

ComboBox<Integer> combo = new ComboBox<>();

    combo.getItems().addAll(1, 2, 3);

    combo.setCellFactory(new Callback<ListView<Integer>, ListCell<Integer>>() {

        @Override

        public ListCell<Integer> call(ListView<Integer> param) {

            return new ListCell<Integer>(){

                @Override

                protected void updateItem(Integer item, boolean empty) {

                    if(item != null)

                        setText("#" + item);

                }

            };

        }

    });

However, when I do this, the customized String is not used for the selected Item (rather the standard toString() value of the item). Apart from that it is no longer selectable using the mouse (I submitted a bug report for this at https://javafx-jira.kenai.com/browse/RT-37445) but that is not really the point of my question.

What am I missing? Is this not the way to do it? Anything else I have to do to make sure the selected item is also rendered using the custom cell or is there a different mechanism for this?

Thanks,

Robert

This post has been answered by James_D on Jun 9 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 8 2014
Added on Jun 8 2014
7 comments
3,159 views