Monday, October 17, 2011

LWUIT background with border

I wanted to separate the list items by a simple horizontal line. So I had created a custom border, which simply draws a line at the bottom of the component. This list itself did not have any background color, i wanted to show the Form behind it, so I had set the background transparency to 0 for the list.

Recently we had a change in the UI design and i needed to add a gradient. And surpise!! Background is not getting drawn. I changed the transparency, of course, changed the color using res editor, then for testing changed in the code.. no luck!

Then I started checking how a component paints its background...

   protected void paintBackground(Graphics g) {
        if (isBorderPainted()) { 
        // isBorderPainted()  =   getStyle().getBorder() != null;

            Border b = getBorder();
            if (b != null && b.isBackgroundPainter()) {
                b.paintBorderBackground(g, this);
        if (getStyle().getBgPainter() != null) {
            getStyle().getBgPainter().paint(g, bounds);

Ok!! As you can see, if there is a border, border is responsible to paint the background (so that it can take care of rounded corners etc nicely?). And obviously i didnt notice that when I was implementing that simple line border.

I quickly fixed my problem by changing the ListMod class (overridden from LWUIT list)


class ListMod extends List {

 * Normally when there is border, background painting is done by border.
 * This flag is used to override that default behavorio. False means background is painted by Style, rather than by Border.
private boolean paintBackgroundByBorder = false;
protected void paintBackground(Graphics g) {
 if (paintBackgroundByBorder) {
 } else {
    if (getStyle().getBgPainter() != null) {
            getStyle().getBgPainter().paint(g, getBounds());


No comments:

Post a Comment