JTable添加单元格值

时间:2014-12-24 05:46:51

标签: java swing jtable defaulttablemodel

//告诉这是否需要更多信息

我想在列的单元格中添加整数。 这是我的代码:

// it's in a mouse listener, eveything works fine, but what I want to do is to add
// the values (for ex. 300,400,500) 

    public void mouseClicked(MouseEvent e) {

    if(e.getClickCount() == 2) {
        JTable tbl = (JTable)e.getSource();
        int selectedRow = tbl.getSelectedRow();
        int tbl1_col = 0; // table 1's column to get the data from
        int tbl2_col = 0; // table 2's column to put the data to

   ((DefaultTableModel)tbl2.getModel()).addRow(new Object[] {null,null,null,null}); 
   //to automatically add a new row

    while(tbl1_col<tbl1.getColumnCount()) {
            tbl2.setValueAt(tbl1.getValueAt(selectedRow,tbl1_col),tbl2_row,tbl2_col);
            // Setting the value to be put on to table 2 from table 1

            System.out.println(tbl2.getValueAt(tbl2_row,3));

            tbl1_col++;
            tbl2_col++;

                if(rcol==tbl1.getColumnCount()){
                            tbl2_row++;
                }

                label.setText(String.valueOf(totalAmount));
     }
}

那边有一个System.out.println,看看里面有什么值 产出是:

null null null 300

我添加值的代码:

totalAmount = totalAmount + (int) tbl2.getValueAt(tbl2_row,3));

我得到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

0 个答案:

没有答案