JTable在第一次执行时不显示结果

时间:2015-04-26 14:18:29

标签: java jtable

代码是将数据添加到JTable。

            String [] header = new String [] {"No","Customer","Video","Date","Days","Status-Closed"};
            Contract [] contractArray = contractList.toArray(new Contract [contractList.size()]);
            Object [][] datarow = new Object [contractArray.length][6];

            try{
               //Read the objects from file and store in the List
                ObjectInputStream inputStream = new ObjectInputStream(
                          new FileInputStream("contractfile"));
                Object o = inputStream.readObject();

                while(o != null){
                    Contract c = (Contract) o;
                    contractList.add(c);
                    o = inputStream.readObject();
                }

            inputStream.close();                

            } catch (EOFException ex) {
                //
            } 

            //Push data to the array
            int no = 0; 
            for(int i = 0; i < contractArray.length; i++){
                no++;
                datarow[i][0] = no;
                datarow[i][1] = contractArray[i].getAccount().getName();
                datarow[i][2] = contractArray[i].getVideo().getName();
                datarow[i][3] = contractArray[i].getDate();
                datarow[i][4] = contractArray[i].getDay();
                datarow[i][5] = contractArray[i].getClosed();
            }

            //set the table model
            TableModel mod = new DefaultTableModel(datarow,header);
            table.setModel(mod);

第一次单击按钮时,表格只显示标题,第二次将数据放入表格中。调试时,datarow[][]第一次没有元素,第二次有所有对象。 为什么,有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

在从文件加载列表之前调用Contract [] contractArray = contractList.toArray(new Contract [contractList.size()]);。所以size()== 0。

之后datarow = new Object [contractArray.length][6];,但长度为0