JAVA中在Jtable中增加一列按钮,并且点击按钮能捕捉事件。应该怎么增加?

JAVA中在Jtable中增加一列按钮,并且点击按钮能捕捉事件。应该怎么增加?,第1张

import java.awt.Component\x0d\x0aimport java.awt.event.ActionEvent\x0d\x0aimport java.awt.event.ActionListener\x0d\x0a\x0d\x0aimport javax.swing.*\x0d\x0aimport javax.swing.table.*\x0d\x0a\x0d\x0apublic class ButtonTable extends JFrame{\x0d\x0a\x0d\x0aprivate static final long serialVersionUID = 1L\x0d\x0aprivate JTable table = null\x0d\x0aprivate DefaultTableModel model=null\x0d\x0aprivate JScrollPane js=null \x0d\x0apublic ButtonTable(){\x0d\x0amodel = new DefaultTableModel(3,3)\x0d\x0atable = new JTable(model)\x0d\x0atable.getColumnModel().getColumn(1).setCellEditor(new MyRender())//设置编辑器\x0d\x0atable.getColumnModel().getColumn(1).setCellRenderer(new MyRender() )\x0d\x0ajs = new JScrollPane(table)\x0d\x0a\x0d\x0athis.add(js)\x0d\x0athis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)\x0d\x0athis.setVisible(true)\x0d\x0athis.setSize(399, 300)\x0d\x0athis.setLocationRelativeTo(null)\x0d\x0a}\x0d\x0a\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0anew ButtonTable()\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a//渲染 器 编辑器\x0d\x0aclass MyRender extends AbstractCellEditor implements TableCellRenderer,ActionListener, TableCellEditor{\x0d\x0a\x0d\x0aprivate static final long serialVersionUID = 1L\x0d\x0aprivate JButton button =null\x0d\x0apublic MyRender(){\x0d\x0abutton = new JButton("确定不?")\x0d\x0abutton.addActionListener(this)\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Object getCellEditorValue() {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn null\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Component getTableCellRendererComponent(JTable table, Object value,\x0d\x0aboolean isSelected, boolean hasFocus, int row, int column) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn button\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic void actionPerformed(ActionEvent e) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aJOptionPane.showMessageDialog(null, "渲染器学期", "消息", JOptionPane.OK_OPTION)\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Component getTableCellEditorComponent(JTable table, Object value,\x0d\x0aboolean isSelected, int row, int column) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn button\x0d\x0a}\x0d\x0a\x0d\x0a}

这个可以用MouseListener来监听。

table.addMouseListener(....)

//括号内的东西根据实际填写

……

public

void

mouseClicked(MouseEvent

e)

{

int

index=table.getSelectedRow()

String

code=(String)table.getValueAt(index,0)//这一句是取出表格选中那行的第一列

text1.setText(code)

//其他列的数据类似,不逐一说明,自己研究即可。

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11408247.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存