Need a function that adds a new record to TableItem using TableModel in J2ME

こ雲淡風輕ζ 提交于 2019-12-10 22:59:27

问题


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


回答1:


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

    }



回答2:


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


来源:https://stackoverflow.com/questions/5271399/need-a-function-that-adds-a-new-record-to-tableitem-using-tablemodel-in-j2me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!