日期:2014-05-20  浏览次数:20945 次

关于Jtable行选中事件的疑问
我现在想实现的效果是点击JTable的某行,便可以触发事件,将该行的内容显示出来,我的事件监听代码是这样的:

@Override
public void valueChanged(ListSelectionEvent e)
{
int rowNum = attributeTable.getSelectedRow();
if(rowNum != -1)
{
((DefaultTableModel)(attributeTable.getModel())).getDataVector();

}
}

然后当我选中某一行后,进入该方法获取到得rowNum一直为-1,这个是什么原因啊?

------解决方案--------------------
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) { 
if(jTable.getValueAt(jTable.getSelectedRow(),0)!=null)
{
String s = (String) jTable.getValueAtjTable.getSelectedRow(),0); //获取所选中的行的第一个位置的内容,当然你也可以指定具体的该行第几格
}
jtextfield.setText(s);
repaint();
}
});