添加按钮到jtable [英] Adding button to a jtable

查看:925
本文介绍了添加按钮到jtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,我必须添加一个JButton。

I am having a table in which i have to adda JButton.

我在做什么

TableColumnModel colModel = table.getColumnModel();
    colModel.getColumn(0).setCellEditor(new MYCellEditor(new JCheckbox()));

MyCellEditor extends DefaultCellEditor{

public MyCellEditor(JCheckbox checkbox){

  super(checkbox);
 Jbutton button = new JButton("Start");
 //actionlistener for button.
 }

 }
 MyRenderer extends DefaultTablecellRenderer{

   public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    //return a button for column ==0     
  } 

我的理解是Celleditor对列中的所有单元格都有相同的按钮实例。因此,如果我单击一个按钮,文本将从开始更改为停止,但如果我单击其他行中的按钮则无法正常工作..调试后显示文本已被删除。

My understanding is that the Celleditor has same instance of button for all cells in a column. So if i click on one button the text changes from "Start" to "stop" but if i click on button in other row it doesnt work.. After debugging it shows that the text is alreadt Stop .

我如何在每一行中有不同的按钮实例?

How can i have different instance of button in each row ?

推荐答案

文章 表按钮列 之前的答案提供了更灵活的解决方案,但您可以找到教程 如何使用表:使用其他编辑器 也很有帮助。 ColorEditor 讨论了部分 TableDialogEditDemo ,可通过 Java Web Start 。您需要更改相应的 ColorRenderer

The article Table Button Column cited in @camickr's previous answer provides a more flexible solution, but you may find the tutorial How to Use Tables: Using Other Editors helpful, too. The ColorEditor discussed there is part of the TableDialogEditDemo, available via Java Web Start. You'll need to change the corresponding ColorRenderer accordingly.

这篇关于添加按钮到jtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆