Wednesday, May 25, 2011

Clicking Within A Cell Renderer

One of the things people looked for quite often in Lists was the ability to click on a specific element within a ListCellRenderer to get an event specific to that element. E.g. a list with X's drawn on the side to remove the element.
In the past we contended that since the list is stateless its impossible for us to provide such information and recommended people use a Container with items. We now have a solution which works only for touch devices, this feature allows adding a listener to a component within the renderer which will now trigger an event if the component is touched.
Notice that the component will not contain the right data since the list selection indicates the actual selection, however if you add a listener to a button containing the X you should get an action event on that button before the list action event. This will allow you to toggle a flag indicating that the X was pressed and not a different portion of the renderer.
This isn't ideal but since the list is stateless this is the best we could come up with.

Assuming you used the GUI builder & GenericListCellRenderer you can use the method GenericListCellRenderer.extractLastClickedComponent() in the action listener for the list to get the button that was pressed to generate this event or null if no button was pressed.

I apologize for the lack of videos recently, we are working very hard towards a LWUIT release in June (hopefully, bureaucracy is always our foe) so I don't have quite the same amount of cycles to dedicate for proper blogging. I'll try to get something decent going soon.


  1. Hello Shai,
    I make LWUIT List with Image and that list contains series of Images. When I click item at that List, I handle this event by ActionEvent. But I meet difficult when List is lower than 7 items(I mean 7 items in List), the index (that return from list.getSelectedIndex() method) is start with 1 not 0. And I also meet difficult about when I press last item in that list doesn't generate any events. That all problems occur only when LWUIT list item is lower than 7 items. Please help me what is going on LWUIT list. I develop J2ME application with LWUIT. That is great UI support for me. I like it very much but jar size is big. Now my application jar size is 900kb (that is too big for many mobile device). And now I meet list problem, I instead use Button for list because my list have only 3 items. If you are free, please help me for LWUIT list. Thank You.

  2. I haven't ran into these issues. Check that if the list is within the CENTER of a border layout that the form its in isn't scrollable. Otherwise please file an issue with code that reproduces the problem.

    When obfuscating the size of the JAR will go down to 200kb or lower. Obfuscation removes all the unused code.