代码之家  ›  专栏  ›  技术社区  ›  harshit

向jtable添加按钮

  •  2
  • harshit  · 技术社区  · 16 年前

    我有一个表,我必须在其中添加一个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对一列中的所有单元格都有相同的button实例。所以,如果我点击一个按钮,文本从“开始”变为“停止”,但如果我点击另一行的按钮,它就不起作用了。。调试后显示文本为alreadt Stop。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    文章 Table Button Column 在@camickr之前的 answer 提供了一个更灵活的解决方案,但是您可以找到教程 How to Use Tables: Using Other Editors ColorEditor 这里讨论的是 TableDialogEditDemo ,可通过 Java Web Start ColorRenderer 相应地。