1

I want create a set of buttons with Painter. I wrote next code

class ListButton extends Button{
int id;
    ListButton(int id, final Image unsel, final Image sel, final Image pres) {
        this.id = id;
        getUnselectedStyle().setBgTransparency(255);
        getSelectedStyle().setBgTransparency(255);
        getPressedStyle().setBgTransparency(255);            
        getUnselectedStyle().setAlignment(Component.LEFT);
        getSelectedStyle().setAlignment(Component.LEFT);
        getPressedStyle().setAlignment(Component.LEFT); 
        getUnselectedStyle().setBgPainter(new Painter(){

            public void paint(Graphics graphics, Rectangle rectangle) {

                graphics.drawImage(buttonBgImage, 0, 0);
                int w= rectangle.getSize().getWidth();
                int h= rectangle.getSize().getHeight();
                graphics.drawImage(unsel, w- unsel.getWidth()-10, (h- unsel.getHeight())/2+ 3);
            }
        });
        getSelectedStyle().setBgPainter(new Painter(){

            public void paint(Graphics graphics, Rectangle rectangle) {
                graphics.drawImage(buttonBgImage, 0, 0);
                int w= rectangle.getSize().getWidth();
                int h= rectangle.getSize().getHeight();
                graphics.drawImage(sel, w- sel.getWidth()-10, (h- sel.getHeight())/2+ 3);
            }
        });
        getPressedStyle().setBgPainter(new Painter(){

            public void paint(Graphics graphics, Rectangle rectangle) {
                graphics.drawImage(buttonBgImage, 0, 0);
                int w= rectangle.getSize().getWidth();
                int h= rectangle.getSize().getHeight();
                graphics.drawImage(pres, w- pres.getWidth()-10, (h- pres.getHeight())/2+ 3);
            }
        });

    }

}

If I insert 2 buttons into the Form only first button is showed well. Second button is without background image (buttonBgImage) and without icon (sel, unsel or pres). I found randomly that second button will be painted if it will be inserted in some Container. What the strange behavior? Sorry for my English.

Vladimir
  • 21
  • 2

1 Answers1

1

List has a specific optimization for Renderers/Painters in place that breaks this. We generally recommend people stick with Styles and UIID manipulation and avoid using painters for tasks like these.

E.g. in Codename One/LWUIT we even have specific support in the GUI builder for pinstripe UI in the list renderer.

If you insist on this approach try using list.setMutableRendererBackgrounds(true); to disable this optimization.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65