當前位置:編程學習大全網 - 編程語言 - 用java做swing,在jtable壹列裏加了按鈕,用來點擊刪除這壹列的數據,操作相應的數據庫,但是如何寫按鈕監

用java做swing,在jtable壹列裏加了按鈕,用來點擊刪除這壹列的數據,操作相應的數據庫,但是如何寫按鈕監

其實這按鈕只是壹個樣式。也可以是壹個字符串。那麽妳要監聽的是妳的jtable

給妳的jtable添加壹個MouseListener

在實現MouseListener 的類中有壹個方法 以下代碼是我原來做的妳可以參考以下

public void mousePressed(MouseEvent e)

{

if (e.getClickCount() >= 2)

{

int row = dataTable.rowAtPoint(e.getPoint());

int column = dataTable.columnAtPoint(e.getPoint());

if (column == 4)// 顯示remark 字段

{

JOptionPane.showMessageDialog(this, dataTable.getValueAt(row, column), "備註詳細", 1);

}

else if (column == 5)// 詢問是否刪除

{

String date = (String) dataTable.getValueAt(row, 0);

String type1 = (String) dataTable.getValueAt(row, 1);

String type2 = (String) dataTable.getValueAt(row, 2);

String fee = (String) dataTable.getValueAt(row, 3);

String remark = (String) dataTable.getValueAt(row, 4);

String idKey = (String) dataTable.getValueAt(row, 5);

String info = date + "\n" + type1 + "\n" + type2 + "\n" + fee + "\n" + remark;

int confirm = JOptionPane.showConfirmDialog(this, info, "是否刪除?",

JOptionPane.YES_NO_OPTION);

if (confirm == 0)

{

try

{

AccountService.delete(date, idKey);

if (date == null || "".equals(date))

{

date = Util.getToday();

}

list = AccountService.getOneDayInfo(date);

dataScrollPane.setViewportView(getDataTable(list));

}

catch (Exception e1)

{

JOptionPane.showMessageDialog(this, "刪除異常");

}

}

}

}

}

  • 上一篇:計算機專業畢業後可以從事哪些工作?
  • 下一篇:在職研究生和全日制研究生有什麽區別,學歷最終壹樣嗎
  • copyright 2024編程學習大全網