简体   繁体   中英

JTable does not show the result on the first execution

The code is to add the data to the 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);

The first time I click the button, the table just show the header, click the second time the data was put into the table. When debugging, the first time the datarow[][] has no element, the second time it had all the objects. Why, could anyone help me out please??

You call Contract [] contractArray = contractList.toArray(new Contract [contractList.size()]); before loading the list from file. So the size() == 0.

After that datarow = new Object [contractArray.length][6]; but the length is 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM