4

How to create a Dynamic table in lwuit

TableModel model = new DefaultTableModel(
                new String[]{"A", "B", "Call Avg"},
                new Object[][]{
                    {"0", "50", "0.00"},
                    {"0", " " + "2", "0.00"},
                    {"0", "52", "0.00"},}) 
{

            public boolean isCellEditable(int row, int col) {
                return col != 0 ;
            }
        };
        Table table = new Table(model);

This is for static . I want to create dynamically number of rows and columns .. Plz help

bharath
  • 14,283
  • 16
  • 57
  • 95
JohnRaja
  • 2,377
  • 6
  • 26
  • 47

1 Answers1

7

See this sample code. I have created dynamic table with LWUIT using this code.

    Form form = new Form();
    form.setLayout(new BorderLayout());
    ValueBeans[] valueBeans = new ValueBeans[size];     
    // Here you can use Bean array value. This array contains collection of bean class. 
    // You can get the values from this beans class. 
    // You need to create dynamically with your own staff

    Object[][] arrObj = new Object[valueBeans.length, 3];

    TableModel model = new DefaultTableModel(new String[]{"Column 1", "Column 2", "Column 3"}, arrObj) {

      public boolean isCellEditable(int row, int col) {
        return false; // return true if editable cell
         }
      };

for (int index = 0; index < rowValues.size(); index++) {                                   
    model.setValueAt(index, 0, valueBeans[index].getValue1()); // row , column , value
    model.setValueAt(index, 1, valueBeans[index].getValue2()); 
    model.setValueAt(index, 2, valueBeans[index].getValue3()); 
}

    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);
    c.setFocusable(false);
    return c;
     }
    };
    table.setScrollable(false);
    form.addComponent(BorderLayout.CENTER, table);
bharath
  • 14,283
  • 16
  • 57
  • 95
  • What packages do I need to import in that case?? I haven't used LWUIT before and now I have downloaded and using it.... – Suhrob Samiev Mar 14 '11 at 04:06
  • @Suhrob: I have posted sample code. Here I used bean class for getting the values dynamically in the table. You have to create the bean class or you can use some other technique also. – bharath Mar 14 '11 at 05:23
  • Hello @bhakki I am working on a project to develop Flight Information Display System and I have completed it and next thing that I wanna do is displaying flight information on mobile phones. That's I parse data using kxml sent by sockets and now i need to dynamically create table model and fill table items. That's why can you help me plz.... to show a working example of how to add dynamically items in table.... Thank you in advance! Also I'd like @user237383 to share your working example – Suhrob Samiev Mar 14 '11 at 09:52
  • How you can getting the values? – bharath Mar 14 '11 at 10:25
  • Please follow this url http://stackoverflow.com/questions/5271399/need-a-function-that-adds-a-new-record-to-tableitem-using-tablemodel-in-j2me – Suhrob Samiev Mar 15 '11 at 09:08