0
votes

I am trying to set a LabelField's background to transparent, and I have search wide for a solution, but nothing I have tried so far seems to be working. Basically my problem is the following, I have a ListField witha a TableRowManager and I am adding the Labels to the table row manager and then drawing them in the table row manager's draw row. But I am instantiating my LabelField's as follow:

LabelField address = new LabelField(e.Address, DrawStyle.ELLIPSIS
                | FOCUSABLE) {

            public void paint(Graphics g) {
                g.setGlobalAlpha(255);
                g.setColor(Color.WHITE);
                g.clear();
                super.paint(g);
            }

            protected void paintBackground(Graphics g) {

                g.setGlobalAlpha(0);
                g.setBackgroundColor(Color.BLACK);
                g.clear();
                super.paint(g);

            }

            public int getPreferredWidth() {
                return Display.getWidth() - 100;
            }

            protected void layout(int maxWidth, int maxHeight) {
                super.layout(getPreferredWidth(), maxHeight);
                setExtent(getPreferredWidth(), getHeight());
            }

        };
        address.setBackground(BackgroundFactory
                .createSolidTransparentBackground(0, 0));

The above gives me a LabelField with a black background which is fine, until I put focus on that row, I can't use black for the onFocus override because I need to give the user some idea of where he is currently focused.

EDIT:

I changed the above code to:

LabelField name = new LabelField(e.Name,
                DrawStyle.ELLIPSIS | FOCUSABLE){

public void paint(Graphics g) {

    g.setColor(Color.WHITE);
    g.clear();
    super.paint(g);
}
};

Because he said in his answer that LabelField and EditField has default transparent background, well you are wrong:

Screenshot of ListField

This is my complete code:

import java.util.Vector;
import net.rim.device.api.collection.List;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class SpecialsListField extends ListField implements ListFieldCallback {

private Bitmap p1;
TableRowManager row;
Vector rows;

public SpecialsListField(List list) {
    /* Init & Declaration */
    setEmptyString("This ListField has No Data", DrawStyle.HCENTER);
    setCallback(this);
    setRowHeight(179);
    Font.getDefault();
    p1 = Bitmap.getBitmapResource("no-image.png");
    rows = new Vector();
    for (int x = 0; x < list.size(); ++x) {
        Special e = (Special) list.getAt(x);

        row = new TableRowManager();

        BitmapField myImageField = new BitmapField(p1);
        row.add(myImageField);

        LabelField name = new LabelField(e.Name, DrawStyle.ELLIPSIS
                | FOCUSABLE) {

            public void paint(Graphics g) {
                g.setGlobalAlpha(255);
                g.setColor(Color.WHITE);
                g.clear();
                super.paint(g);
            }

            protected void paintBackground(Graphics g) {

                g.setGlobalAlpha(0);
                g.setBackgroundColor(Color.BLACK);
                g.clear();
                super.paint(g);

            }
        };

        name.setBackground(BackgroundFactory
                .createSolidTransparentBackground(0, 0));

        row.add(name);

        LabelField date = null;

        if (e.DateFrom.compareTo(e.DateTo) == 0) {
            date = new LabelField(e.DateFrom, DrawStyle.ELLIPSIS
                    | FOCUSABLE) {

                public void paint(Graphics g) {
                    g.setGlobalAlpha(255);
                    g.setColor(Color.WHITE);
                    g.clear();
                    super.paint(g);
                }

                protected void paintBackground(Graphics g) {

                    g.setGlobalAlpha(0);
                    g.setBackgroundColor(Color.BLACK);
                    g.clear();
                    super.paint(g);

                }

            };
            date.setBackground(BackgroundFactory
                    .createSolidTransparentBackground(0, 0));
            row.add(date);
        } else {
            date = new LabelField(e.DateFrom + " - " + e.DateTo,
                    DrawStyle.ELLIPSIS | FOCUSABLE) {

                public void paint(Graphics g) {
                    g.setGlobalAlpha(255);
                    g.setColor(Color.WHITE);
                    g.clear();
                    super.paint(g);
                }

                protected void paintBackground(Graphics g) {

                    g.setGlobalAlpha(0);
                    g.setBackgroundColor(Color.BLACK);
                    g.clear();
                    super.paint(g);

                }

            };
            date.setBackground(BackgroundFactory
                    .createSolidTransparentBackground(0, 0));
            row.add(date);
        }

        LabelField time = null;

        if (e.TimeFrom.compareTo(e.TimeTo) == 0) {
            time = new LabelField(e.TimeFrom, DrawStyle.ELLIPSIS
                    | FOCUSABLE) {
                public void paint(Graphics g) {
                    g.setGlobalAlpha(255);
                    g.setColor(Color.WHITE);
                    g.clear();
                    super.paint(g);
                }

                protected void paintBackground(Graphics g) {

                    g.setGlobalAlpha(0);
                    g.setBackgroundColor(Color.BLACK);
                    g.clear();
                    super.paint(g);

                }

            };

            time.setBackground(BackgroundFactory
                    .createSolidTransparentBackground(0, 0));

            row.add(time);
        } else {
            time = new LabelField(e.TimeFrom + " - " + e.TimeTo,
                    DrawStyle.ELLIPSIS | FOCUSABLE) {

                public void paint(Graphics g) {
                    g.setGlobalAlpha(255);
                    g.setColor(Color.WHITE);
                    g.clear();
                    super.paint(g);
                }

                protected void paintBackground(Graphics g) {

                    g.setGlobalAlpha(0);
                    g.setBackgroundColor(Color.BLACK);
                    g.clear();
                    super.paint(g);
                }

            };
            time.setBackground(BackgroundFactory
                    .createSolidTransparentBackground(0, 0));
            row.add(time);
        }

        LabelField address = new LabelField(e.Address, DrawStyle.ELLIPSIS
                | FOCUSABLE) {

            public void paint(Graphics g) {
                g.setGlobalAlpha(255);
                g.setColor(Color.WHITE);
                g.clear();
                super.paint(g);
            }

            protected void paintBackground(Graphics g) {

                g.setGlobalAlpha(0);
                g.setBackgroundColor(Color.BLACK);
                g.clear();
                super.paint(g);

            }

            public int getPreferredWidth() {
                return Display.getWidth() - 100;
            }

            protected void layout(int maxWidth, int maxHeight) {
                super.layout(getPreferredWidth(), maxHeight);
                setExtent(getPreferredWidth(), getHeight());
            }

        };
        address.setBackground(BackgroundFactory
                .createSolidTransparentBackground(0, 0));
        row.add(address);

        rows.addElement(row);
    }
    setSize(rows.size());
}

public void drawListRow(ListField listField, Graphics graphics, int index,
        int y, int width) {
    // TODO Auto-generated method stub
    SpecialsListField list = (SpecialsListField) listField;
    TableRowManager rowManager = (TableRowManager) list.rows
            .elementAt(index);
    rowManager.drawRow(graphics, 0, y, width, 179);
}

protected void drawFocus(Graphics graphics, boolean on) {
    // get the focus rect area
    XYRect focusRect = new XYRect();
    getFocusRect(focusRect);

    boolean oldDrawStyleFocus = graphics
            .isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
    try {
        if (on) {
            // set the style so the fields in the row will update its color
            // accordingly
            graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
            int oldColour = graphics.getColor();
            try {
                graphics.setColor(Color.DARKGRAY); // set the color and draw
                                                    // the
                                                    // color
                graphics.fillRect(focusRect.x, focusRect.y,
                        focusRect.width, focusRect.height);
            } finally {
                graphics.setColor(oldColour);
            }
            // to draw the row again

            drawListRow(this, graphics, getSelectedIndex(), focusRect.y,
                    focusRect.width);

        }

    } finally {
        graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS,
                oldDrawStyleFocus);
    }
}

public Object get(ListField listField, int index) {
    // TODO Auto-generated method stub
    return rows.elementAt(index).toString();
}

public int getPreferredWidth(ListField listField) {
    // TODO Auto-generated method stub
    return Graphics.getScreenWidth();
}

public int getRowHeight() {
    // TODO Auto-generated method stub
    return 179;
}

public int getRowHeight(int arg0) {
    // TODO Auto-generated method stub
    return 179;
}

public int indexOfList(ListField listField, String prefix, int start) {
    // TODO Auto-generated method stub
    return -1;
}

protected boolean trackwheelClick(int status, int time) {
    int index = getSelectedIndex();
    Dialog.inform(Integer.toString(index));
    return true;
}

private class TableRowManager extends Manager {
    public TableRowManager() {
        super(0);
    }

    // Causes the fields within this row manager to be layed out then
    // painted.
    public void drawRow(Graphics g, int x, int y, int width, int height) {
        // Arrange the cell fields within this row manager.
        layout(width, height);
        // Place this row manager within its enclosing list.
        setPosition(x, y);
        // Apply a translating/clipping transformation to the graphics
        // context so that this row paints in the right area.
        g.pushRegion(getExtent());
        // Paint this manager's controlled fields.
        subpaint(g);
        /*
         * g.setColor(0xFF0000); g.drawLine(0, height - 1,
         * getPreferredWidth(), height - 1); g.drawLine(40, 0, 40,
         * getPreferredHeight());
         */
        // Restore the graphics context.
        g.popContext();
    }

    // Arrages this manager's controlled fields from left to right within
    // the enclosing table's columns.
    protected void sublayout(int width, int height) {
        // set the size and position of each field.
        int fontHeight = Font.getDefault().getHeight();
        int preferredWidth = getPreferredWidth();
        // start with the Bitmap Field of the priority icon
        Field field = getField(0);
        layoutChild(field, 75, 75);
        setPositionChild(field, 5, 5);
        // set the task name label field
        field = getField(1);
        layoutChild(field, preferredWidth, fontHeight + 2);
        setPositionChild(field, 85, 5);
        // set the list name label field
        field = getField(2);
        layoutChild(field, preferredWidth, fontHeight + 2);
        setPositionChild(field, 85, fontHeight + 6 + 5);
        // set the due time name label field
        field = getField(3);
        layoutChild(field, preferredWidth, fontHeight + 2);
        setPositionChild(field, 100, fontHeight + fontHeight + 8 + 5);

        field = getField(4);
        layoutChild(field, preferredWidth, fontHeight + 2);
        setPositionChild(field, 100, fontHeight + fontHeight + fontHeight
                + 10 + 5);

        setExtent(preferredWidth, getPreferredHeight());
    }

    // The preferred width of a row is defined by the list renderer.
    public int getPreferredWidth() {
        return Graphics.getScreenWidth();
    }

    // The preferred height of a row is the "row height" as defined in the
    // enclosing list.
    public int getPreferredHeight() {
        return 179;// getRowHeight();
    }

}

}
2
Which background color do you want your label field? Transparent? Or black? Also, I can't think of any reason why you should call super.paint(g) from paintBackground(). paint() and paintBackground() are two different methods in the UI drawing model. You probably shouldn't be calling one from the other. Also, please see SSCCE. - Nate
@Nate Event using super.paintBackground() in paintBackground() it is not makng the label background transparent? - Armand
@Nate The first piece of code is the relevant code, I only included the rest for reference. - Armand
I'm not saying that's your only problem. It's just one thing that didn't look right to me. - Nate
I appreciate that the rest of your code is for reference. However, it's not a small, self-contained, compilable example. If you want people to help you for free, it's wise to make it as easy on them as possible to do so. Don't give them a bunch of stuff to read that's not relevant to your problem. Show the class in question. If you really want to include more, build the smallest "Hello World" program that can demonstrate the problem. All that extra stuff is just costing other people time, making it harder to help you. - Nate

2 Answers

0
votes

I said labelfield has transparent background from born, may be not exactly. when labelfield got focus, there IS blue background that can not get rid of, even use this demo code:

a simple and self-contained helloworld example.

final class HelloWorldScreen extends MainScreen{
EditField edt = null;

HelloWorldScreen() {
    // set background to black
    this.getMainManager().setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));

    // use to alternate focus
    edt = new EditField("", "");
    add(edt);

    add(new LabelField("test label tranparent", DrawStyle.ELLIPSIS
            | FOCUSABLE) {

        public void paint(Graphics g) {}

        public void applyTheme(){}

        protected void paintBackground(Graphics g) { }
    });

}

}

I override the paint() applyTheme() and paintBackground(), and do nothing there. However, the labelfield which focused still has a blue background.

yeah, another WTF thing.

may i suggest you use an editfiled with readonly style?

0
votes

To solve the focused blue background all you have to do is override drawFocus and make it empty
protected void drawFocus(Graphics g, boolean on) { } that way nothing is going to be drawn whet the field is focus at least you want it to.