Lemur ListBox custom cell renderer issue


When using my custom cell renderer ListAction does not work

private class CheckableCellRenderer extends DefaultCellRenderer<String> {

        public CheckableCellRenderer() {

        public Panel getView(String value, boolean selected, Panel existing) {
            Panel panel = super.getView(value, selected, null);
            Container container = new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.Even, FillMode.First));
            container.addChild(new Checkbox(""), 1);
            return container;

here the command is never called when I select an item:

listBox.addCommands(ListBox.ListAction.Down, source -> {
            System.out.println("list box action"); // Not called

I guess it is because of the Container somehow prevents action from being detected, but not sure how to fix it.

First, you should really be checking ‘existing’ and reusing it if possible. Else every time you scroll it will create new containers and stuff… maybe even every time you select. It’s likely that extending DefaultCellRenderer doesn’t really help you here as you are overriding most of its existing behavior. For example, it seems like you could write your own CellRenderer that just uses CheckBox instead of Button and be done. No special container handling or anything.

As to the other, I’m not sure why it isn’t clickable. Any explanation I can come up with (like the Button is getting the click first) seems like if true then it would happen all the time and not just in your use-case.

1 Like

Could you please explain this because I think I misinterpret it. Should we keep a reference to the Panel the #getView method returns? I’m using the CellRenderer interface a lot, and I am always createinga new instance in this method.

It’s passing you the previous one you created all the time. You should be reusing it if possible.

For example, DefaultCellRenderer knows its a button and so will just change the text if the ‘existing’ parameter is not null.

1 Like

Thanks for the info, didn’t know this! However the parameter name existing is very clear :man_facepalming:

1 Like