最终效果
鼠标点击JTable中任一数据,修改相应的信息。
确定点击的行和列
package com.dao; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JTable; public class TableMethod extends BaseDAO{ int rowww ; int colll ; public int getRowww() { return rowww; } public void setRowww(int rowww) { this.rowww = rowww; } public int getColll() { return colll; } public void setColll(int colll) { this.colll = colll; } /** 确定行和列*/ public void TableRowCol(final JTable tJTable) { tJTable.setRowSelectionAllowed(true);//设置是否可以选择此模型中的行 tJTable.setColumnSelectionAllowed(true);//设置是否可以选择此模型中的列 tJTable.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent event) { int row = tJTable.rowAtPoint(event.getPoint()); int col = tJTable.columnAtPoint(event.getPoint()); tJTable.setRowSelectionInterval(row, row); tJTable.setColumnSelectionInterval(0, tJTable.getColumnCount()-1); // tJTable.setColumnSelectionInterval(col, col); setRowww(row); setColll(col); } }); } }
在修改按钮中设置监听器
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:修改 tm.TableRowCol(jTable1); if (tm.getColll()==0 && tm.getRowww()==0) { JOptionPane.showMessageDialog(null, "请先选择顾客!", "修改顾客信息失败", 0); } else { String username = (String)tableObjects()[tm.getRowww()][1]; Manage_Adduser mAdduser = new Manage_Adduser(username); mAdduser.setVisible(true); } }
其中
tableObjects数组是JTable的数据转换得到的二维数组。