Suhrob Samiev
Suhrob Samiev

Reputation: 1547

adding an image component to Table cell by overidding `createCell`

I am using LWUIT and showing data with Table, say, flight information! Instead of writing air companies with text I just like to replace them with icons. So, I need to override protected Component createCell(Object value, final int row, final int column, boolean editable) method of Table.

This is how I've implemented:

Initializing

imgAln[i]=null;
try {
    imgAln[i] = Image.createImage(strPathToImage[i]);
                        //e.g /uta.png,/somonair.png and so on
    lAln[i] = new Label(imgAln[i]);
} catch (IOException e) { }

Creating Table object

Table table = new Table(model) {
    protected Component createCell(Object value, final int row, 
               final int column, boolean editable) {
        final Component c = super.createCell(value, row, column, editable);
        if (column == 6) {
            return lAln[value];  //it does not work here 
        }
    }
};

need help to add Image to table cell!!!

Is there any example??? links are welcome!

Upvotes: 2

Views: 387

Answers (1)

Vimal
Vimal

Reputation: 1266

The problem in your createCell(...) implementation is that it does not return the super.createCell(...) when the column is not 6. Also your array of labels (lAln) may not be properly created. Try my implementation below, but make sure you store the appropriate image name in the table models' column 0.

This should solve it:

TableModel model = new DefaultTableModel(
    new String[]{"Uneditable", "Editable", "CheckBox", "Multiline"}, 
    new Object[][]{
        {"/animations.png", "", new Boolean(false), "Multi-line text\nright here"},
        {"/buttons.png", "", new Boolean(true), "Further text that\nspans lines"},
        {"/dialogs.png", "", new Boolean(true), "No span"},
        {"/fonts.png", "", new Boolean(false), "Spanning\nFor\nEvery\nWord"},
    });

Table table = new Table(model) {
    protected Component createCell(Object value, final int row, 
                    final int column, boolean editable) {
        if (row != -1 && column == 0) {
            try {
                            //In my case Column 0 store the resource path names
                return new Label(Image.createImage((String)value));     
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return super.createCell(value, row, column, editable);
    }
};

NOTE: If you see the names instead of images in column 0 it means the image path is incorrect, fix it to see the images.

Did you manage to have a look at TableLayoutDemo.java in project LWUITDemo? If i remember it correct, this comes bundled download package LWUIT1.5.zip (or you can always google it).

Let me know if you need more specific help.

Upvotes: 3

Related Questions