JTable鼠标监听器鼠标适配器

时间:2014-12-22 01:45:24

标签: java swing jtable mouselistener

我正在制作销售点系统,我在这里有两个表,tableOne和tableTwo。 我想要做的是单击tableOne中的一行,它将出现在tableTwo

但我的控制台上有错误

"java.lang.ArrayIndexOutOfBoundsException: 4 >= 4"

我的代码工作正常,当我的控制台上显示该错误时,我只是继续单击行并且它可以工作。我该如何删除错误?

// row,col is the row and column in tableOne ( Where we will get the data )
// trow,trow is the row and column in tableTwo  ( Where we will put the data )

public void tebel(){
    table.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            if(e.getClickCount() == 2) {

                target = (JTable)e.getSource();
                int row = target.getSelectedRow();
                int col= 0;
                int trow = 0;
                int tcol = 0;
                if(row>=0){
                    while(tcol<=tableOne.getColumnCount()){
                        tableTwo.setValueAt(tableOne.getValueAt(row,col),trow,tcol);
                        col++;
                        tcol++;
                        if(tcol==tableOne.getColumnCount()){
                            trow++;
                        }

                    }
                }
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

while(tcol<=tableOne.getColumnCount()){

我猜你的情况应该是<