【问题标题】:cannot recive data from model in JTable无法从 JTable 中的模型接收数据
【发布时间】:2013-11-28 10:44:48
【问题描述】:

我在将数据数组列表对象显示到 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 显示新数据

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

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

    查看How to use tables 中的一些示例。

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

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

    【讨论】:

    • 谢谢你亲爱的疯狂程序员!
    【解决方案2】:

    请删除

    table = new JTable( model );
    

    并设置

    table.setModel(model);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 2017-11-25
      相关资源
      最近更新 更多