2011-12-15 100 views
2

我正在使用LWUIT並顯示數據與Table,比如說航班信息! 而不是寫文字的空運公司,我只是想用圖標替換它們。 所以,我需要覆蓋protected Component createCell(Object value, final int row, final int column, boolean editable)方法Table通過添加一個圖像組件添加到表格單元`createCell`

這是怎麼了,我實現:

初始化

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

需要幫助的圖像添加到表格單元格!

有沒有例子?鏈接歡迎!

回答

3

createCell(...)實現中的問題是,當column is not 6時,它不返回super.createCell(...)。此外,您的一系列標籤(lAln)可能無法正確創建。嘗試下面的實現,但請確保將適當的圖像名稱存儲在表模型'column 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圖像這意味着圖像路徑不正確,解決它看到的圖像。

您是否能夠在項目中看到TableLayoutDemo.javaLWUITDemo?如果我記得它是正確的,這是捆綁下載包LWUIT1.5.zip(或者你可以隨時谷歌它)。

讓我知道你是否需要更具體的幫助。

+0

非常感謝你Vimal!完美的工作示例!有用!至於你推薦我的演示例子,我現在仔細查看它!謝謝!這裏是一個截圖http://www.flickr.com/photos/[email protected]/6525971703/in/photostream – 2011-12-17 15:43:57

相關問題