最終效果
鼠標點擊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的數據轉換得到的二維數組。
