通过覆盖 `createCell` 将图像组件添加到表格单元格

发布于 2024-12-21 12:13:48 字数 903 浏览 4 评论 0原文

我正在使用 LWUIT 并使用 Table 显示数据,例如航班信息! 我不喜欢用文字来写航空公司,而是用图标来代替它们。 因此,我需要重写 Tableprotected Component createCell(Object value, Final int row, Final int column, boolean editable) 方法。

这就是我的实现方式:

初始化

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) { }

创建表对象

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 
        }
    }
};

需要帮助将图像添加到表单元格!

有例子吗???欢迎链接!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难得心□动 2024-12-28 12:13:48

createCell(...) 实现中的问题是,当 column 不是 6< 时,它不会返回 super.createCell(...) /代码>。此外,您的标签数组 (lAln) 可能无法正确创建。请尝试下面的实现,但请确保将适当的图像名称存储在表模型的第 0 列 中。

这应该可以解决问题:

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);
    }
};

注意:如果您在第 0 列中看到的是名称而不是图像,则意味着图像路径不正确,请修复它以查看图像。

您是否设法查看了项目LWUITDemo中的TableLayoutDemo.java?如果我没记错的话,这是捆绑的下载包LWUIT1.5.zip(或者你可以随时用谷歌搜索它)。

如果您需要更具体的帮助,请告诉我。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文