Tuesday, October 18, 2011

LWUIT List border

I was again having some interesting problem with LWUIT border. I had different kind of border for the normal list item and focused list item in the UI design. So obviously I had two UIID ListRenderer and ListRendererFocus defined in the theme.

But somehow the focused border was not getting painted !!

I didnt have much time to dig into details, but manually changing the border in ListCellRenderer fixed this.



public class MyListRenderer extends Label implements ListCellRenderer {
    //...

   /** To draw the background of the focused item. */
   private Label focus = new Label("");
   
   /** Border set by lwuit theme. */
   private Border originalBorder;

   public ListChannelRenderer() {
       //..other stuffs...
       originalBorder = getStyle().getBorder();
   }

   /**{@inheritDoc} */
   public Component getListCellRendererComponent
            (List list, Object value, int index, boolean isSelected) {
       //...change label content based on list index

       //When selected, we want to draw the focused border only, 
       // that's why setting to null
       //Lwuit draws only the original background even for focused
      if (list.getSelectedIndex() == index && list.hasFocus()) {
           this.getStyle().setBorder(null);
      } else {
           this.getStyle().setBorder(originalBorder);
      }
      return this;
   }

   /**{@inheritDoc} */
   public Component getListFocusComponent(List list) {
      return focus;
   }
}

No comments:

Post a Comment