2

How to create dynamic table model in J2ME? like from arrays i use kxml parser to parse my data and show that data in table.

Could you please give me directions? source code is welcome as well!!

my bean looks like this

public class FIDS {
    private String FNo;
    private String AirCraft;
    private String OnDate;
    private String Gate;
    private String AirCompany;
    private String Remark;
    private String FTime;
    private String BRegTime;
    private String ERegTime;

    public FIDS(){}

    public void SetFN(String fno){this.FNo=fno;}
    public String GetFNo(){return this.FNo;}

    public void SetAirCraft(String acr){this.AirCraft=acr;}
    public String GetAircraft(){return this.AirCraft;}

    public void SetOnDate(String d){this.OnDate=d;}
    public String GetOnDate(){return this.OnDate;}

    public void SetGate(String g){this.Gate=g;}
    public String GetGate(){return this.Gate;}

    public void SetAirCompany(String ac){this.AirCompany=ac;}
    public String GetAirCompany(){return this.AirCompany;}

    public void SetRemark(String rem){this.Remark=rem;}
    public String GetRemark(){return this.Remark;}

    public void SetFTime(String ft){this.FTime=ft;}
    public String GetFTime(){return this.FTime;}

    public void SetBRegTime(String br){this.BRegTime=br;}
    public String GetBRegTime(){return this.BRegTime;}

    public void SetERegTime(String er){this.ERegTime=er;}
    public String GetERegTime(){return this.ERegTime;}
}

and I'm trying to create an array of objects of my bean and fill it with some data but unable to assign that obj to SimpleTableModel as it's constructor receives String[][] type I'm unable to pass arrObj as the following

FIDS [] fd=new FIDS[5];
for (int i=0;i<5;i++)
{
  fd[i].SetFN("SMR23");
  fd[i].SetAirCraft("B735");
  fd[i].SetAirCompany("Somon Air");
  fd[i].SetFTime("10:00");
  fd[i].SetGate("A");
}

Object[][] arrObj=new Object[fd.length][4];
TableModel model = new SimpleTableModel(arrObj,new String[]{"Column 1", "Column 2", "Column 3"})

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

         for (int index = 0; index < fd.length; index++) {

             // model.setValueAt(index, 0, fd[index].GetFNo().toString());// row , column , value
             // model.setValueAt(index, 1, fd[index].GetAirCompany().toString());
             // model.setValueAt(index, 2, fd[index].GetAirCompany().toString());

         }
TableItem table = new TableItem(getDisplay(), "sdfsdf",model);

and also setValue method of TableModel isn't there as I used SimpleTableModel

bharath
  • 14,283
  • 16
  • 57
  • 95
Suhrob Samiev
  • 1,528
  • 1
  • 25
  • 57

2 Answers2

3

Where you are initialized FIDS? use like this,

   FIDS [] fd=new FIDS[4];
    for (int i=0;i<4;i++)
    {   
        fd[i] = new FIDS();    // initialize here
        fd[i].SetFN("SMR23");
        fd[i].SetAirCraft("B735");
        fd[i].SetAirCompany("Somon Air");
        fd[i].SetFTime("10:00");
        fd[i].SetGate("A");

    }
bharath
  • 14,283
  • 16
  • 57
  • 95
  • 1
    THANK YOU VERY MUCH @BHAKKI I AM GRATEFULL FOR YOUR HELP AND SUPPORT! AFTER INITIALIZATION IT"S WORKING PERFECTLY. FINALLY I'M DONE WITH THE PART OF MY PROJECT WITH YOUR HELP! – Suhrob Samiev Mar 16 '11 at 03:42
0

I've added LWUIT to my resources and just coded like that and when I ran this application nothing appeared on screen except it beeped though it has compiled well without errors.

import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.*;
import com.sun.lwuit.table.*;
import com.sun.lwuit.layouts.*;


public class Main extends javax.microedition.midlet.MIDlet{
    public void startApp() {
        Display.init(this);

         FIDS [] fd=new FIDS[4];
            for (int i=0;i<4;i++)
            {
                fd[i].SetFN("SMR23");
                fd[i].SetAirCraft("B735");
                fd[i].SetAirCompany("Somon Air");
                fd[i].SetFTime("10:00");
                fd[i].SetGate("A");

            }
        Object[][] arrObj=new Object[fd.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 < fd.length; index++) {
            model.setValueAt(index, 0, fd[index].GetFNo()); // row , column , value
            model.setValueAt(index, 1, fd[index].GetAircraft());
            model.setValueAt(index, 2, fd[index].GetFTime());
        }

        Form form = new Form();
        form.setTitle("My table");
        form.setLayout(new BorderLayout());

        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(true);


        form.addComponent(BorderLayout.CENTER, table);
        form.show();

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}
Suhrob Samiev
  • 1,528
  • 1
  • 25
  • 57