无法从JTable中的模型接收数据

时间:2013-11-28 10:44:48

标签: java swing jtable

我在显示数据时遇到问题Array List<>反对JTable

public class SearchPatient extends JInternalFrame implements KeyListener 
{
    patientDetailBean pdb=new patientDetailBean();
    ArrayList<patientDetailBean> ap=new ArrayList<patientDetailBean>();
    String[] columnNames = {"Ptn No",
            "Name",
            "Gender",
            "City",
            "ContactNo",
            "Birth-Date",
            "MaritalStatus",
            "Occupation",
            "BloodGroup",
            "TimeOfRegistration"
            };
    DefaultTableModel dm;
    DefaultTableModel model;

    public SearchPatient()
    {
        //other initilization and displaying 

        ap=pdb.getAllPatientDetail();
        setTableData(ap);

        //other initilization, displaying and KeyListener and all working properly 
    }
    private void setTableData(ArrayList<patientDetailBean> arrayp) 
    {
        int lenap=arrayp.size();
        Object[][] data=new Object[lenap][10];      // creating dynamically array of object

        dm = (DefaultTableModel)table.getModel();   //creating model from table

        //removing all row from existing data in table
        while(table.getRowCount()>0) 
        { 
             ((DefaultTableModel) table.getModel()).removeRow(0); 
        }

        //getting data from arrayp ArrayList<patientDetailBean> and save it to array of object(data)
        int in=0;
        for(Object obj:arrayp)
        {
            patientDetailBean pdb1=new patientDetailBean();
            pdb1=(patientDetailBean) obj;
            data[in][0]=pdb1.getPatientid();
            data[in][1]=pdb1.getFirstname()+" "+pdb1.getMiddlename()+" "+pdb1.getSurname();
            data[in][2]=pdb1.getGender();
            data[in][3]=pdb1.getCity();
            data[in][4]=pdb1.getContactno();
            data[in][5]=pdb1.getBirthdate();
            data[in][6]=pdb1.getMaritalstatus();
            data[in][7]=pdb1.getOccupation();
            data[in][8]=pdb1.getBloodgroup();
            data[in][9]=pdb1.getDatetimeofcase();
            in++;
        }

        //creating model from array of object(data)
        model = new DefaultTableModel(data, columnNames);

        //save data to table
        table = new JTable( model );
    }

    public void keyTyped(KeyEvent key) 
    {
        //This is working proper 
        if(key.getSource().equals(txtpname))
        {
            ap=pdb.getAllPatientDetail();

            patientDetailBean pdb=new patientDetailBean();
            ArrayList<patientDetailBean> arr=new ArrayList<patientDetailBean>();
            arr.clear();
            for(Object obj:ap)
            {   
                pdb=(patientDetailBean) obj;

                //getting data from object 
                String str1=(pdb.getFirstname()+" "+pdb.getMiddlename()+" "+pdb.getSurname()).toLowerCase();

                //getting data which user entered(user want to search)  
                String str2=(txtpname.getText()+key.getKeyChar()).toLowerCase();

                if(str1.contains(str2)) // checking for data equal and contain which user typed in JTextField(txtpname) with str1
                {

                    arr.add(pdb);   //save data to ArrayList<patientDetailBean>
                }
            }

            //set recived ArrayList<> object to 
            setTableData(arr);
        }
    }

}

1)第一次运行此代码时,比Jtable中获取的数据正确。    当我在那时键入textField的任何键时,数据正在从表中正确清除,但它没有向JTable显示新数据

2 个答案:

答案 0 :(得分:3)

基本上,您正在创建一个新的JTable,但您不会将其添加到任何内容中。

看看How to use tables的一些例子。

您还应该考虑简单地创建一个新的表模型并将其应用于表(使用setModel)而不是尝试更新现有模型,我认为您会发现它更快更简单地处理。

我还建议直接对行数据进行建模,而不是将每个属性添加到数组中并使用它来表示行,您应该将每个患者对象添加到表模型中,这组成一行。你需要使用AbstractTableModel,但是通过表格编辑数据会让生活更轻松...恕我直言

答案 1 :(得分:2)

请删除

table = new JTable( model );

并设置

table.setModel(model);
相关问题