【问题标题】:JTable Mouse Listener Mouse Adapter [closed]JTable 鼠标侦听器鼠标适配器 [关闭]
【发布时间】:2014-12-22 01:45:23
【问题描述】:

我正在制作一个销售点系统,我在这里有两张桌子,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) 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。 2) 全部小写的单词难以阅读,例如试图听某人喃喃自语。请在句子的开头使用大写字母来表示单词 I,以及像 ArrayList 或 Oracle 这样的专有名称。

标签: java swing jtable mouselistener


【解决方案1】:
while(tcol<=tableOne.getColumnCount()){

我猜你的条件应该是&lt;

【讨论】:

  • 您好,感谢您的回答。与我的代码相比,Listselectionlistener 更可取吗?还是我的代码也可以?
  • @proton32 如果逻辑基于单击鼠标,则将使用 ListSelectionListener。当您单击行或使用箭头键移动到新行时,将调用侦听器。由于您正在侦听双击,因此 MouseListener 是要走的路。但是,您可能还希望添加支持以处理“Enter”键来处理行。查看List Action。那里提供的代码用于 JList,但概念是相同的,只需将类更改为使用 JTable 而不是 JList。
猜你喜欢
  • 2013-03-14
  • 2011-12-18
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 2012-04-05
  • 2013-06-22
相关资源
最近更新 更多