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

 我来答
百度网友adebb714d
推荐于2019-09-13 · TA获得超过217个赞
知道小有建树答主
回答量:384
采纳率:0%
帮助的人:179万
展开全部
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.*;

public class ButtonTable extends JFrame{

private static final long serialVersionUID = 1L;
private JTable table = null;
private DefaultTableModel model=null;
private JScrollPane js=null ;
public ButtonTable(){
model = new DefaultTableModel(3,3);
table = new JTable(model);
table.getColumnModel().getColumn(1).setCellEditor(new MyRender());//设置编辑器
table.getColumnModel().getColumn(1).setCellRenderer(new MyRender() );
js = new JScrollPane(table);

this.add(js);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(399, 300);
this.setLocationRelativeTo(null);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new ButtonTable();
}

}

//渲染 器 编辑器
class MyRender extends AbstractCellEditor implements TableCellRenderer,ActionListener, TableCellEditor{

private static final long serialVersionUID = 1L;
private JButton button =null;
public MyRender(){
button = new JButton("确定不?");
button.addActionListener(this);
}

@Override
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return null;
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// TODO Auto-generated method stub
return button;
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "渲染器学期", "消息", JOptionPane.OK_OPTION);

}

@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
// TODO Auto-generated method stub
return button;
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式