0
    public static void main(String[] args)
 {
        // TODO Auto-generated method stub

        Object[][] tdata ={new Object[]{"1","b","e","f"},new Object[]{"2","*","3","4"},new Object[]{"3","@","#","$"}};
        Object[] tname = {"#1","#2","#3","#4"};
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.setDataVector(tdata, tname);    
        JTable jta = new JTable(dtm);
        jta.setRowSelectionAllowed(false);
        jta.getColumnModel().getColumn(0).setCellEditor(new MButtonEditor());
        JFrame jfr = new JFrame();
        jfr.setSize(800, 800);
        jfr.setLayout(new FlowLayout());
        jfr.add(new JScrollPane(jta));
        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfr.setVisible(true);
    }

class MButtonEditor extends DefaultCellEditor{

    private int cur_row;
    private int cur_col;
    private JTable cur_tab;
    private JButton jbut;
    MActionListener mactl;
    public MButtonEditor() 
    {
        super(new JTextField());
        this.setClickCountToStart(1); 
        initButton();

    }
    private void initButton()
    {
        jbut = new JButton();

    }

    @Override  
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
        cur_row = row;
        cur_col = column;
        cur_tab = table;
        mactl = new MActionListener(this.cur_row,this.cur_col,this.cur_tab);
        jbut.addActionListener(mactl);
        this.jbut.setText(value == null ? "" : String.valueOf(value));

        return jbut;
    }
    @Override  
    public Object getCellEditorValue()  
    {  
        return this.jbut.getText();  
    } 


}
class MActionListener implements ActionListener
{

    private int cur_row;
    private int cur_col;
    private JTable cur_tab;
    DefaultTableCellRenderer backGroundColor;

    MActionListener(int row,int column,JTable table)
    {
        cur_row = row;
        cur_col = column;
        cur_tab = table;
        backGroundColor = new DefaultTableCellRenderer();
        backGroundColor.setBackground(Color.red);
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        cur_tab.getColumnModel().getColumn(1).setCellRenderer(backGroundColor);
        DefaultTableModel dtm = (DefaultTableModel) cur_tab.getModel();
        dtm.removeRow(cur_row);
        System.out.println(dtm.getColumnCount());

    }
}

Stack Trace:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Vector.java:470)
    at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:665)
    at javax.swing.JTable.setValueAt(JTable.java:2741)
    at javax.swing.JTable.editingStopped(JTable.java:4723)
    at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:141)
    at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:368)
    at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:233)
    at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1010)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6513)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
  • 1
    I seriously don't know why people do this, this way. If you must, take a look at [Table Button Column](http://tips4java.wordpress.com/2009/07/12/table-button-column/). Consider using a Key Binding instead and simply remove the selected rows or a toolbar (or other external) button which does the same thing, deletes the selected items. Consider the user experience if you have 100+ rows...:P – MadProgrammer Dec 02 '14 at 05:18
  • Also consider [this example](http://stackoverflow.com/questions/25070511/add-jbutton-to-each-row-of-a-jtable/25071138#25071138) – MadProgrammer Dec 02 '14 at 05:20
  • @MadProgrammer,i am sorry,i am just a beginer:(,thanks for you comment!i just can not understand how the exceptions happen..... – user3125485 Dec 02 '14 at 05:34
  • I could tell you the reason the problem is occurring, but my fear is, you will continue to use this approach, rather than exploring more appropriate methods – MadProgrammer Dec 02 '14 at 22:33
  • @MadProgrammer i read the article from the link above and i know it is more friendly for users to use menu:),and i tried to do that way.but at the same time ,i want to figure out how my old code go wrong,,,,i will really appreciate it if you can tell me the reason! – user3125485 Dec 02 '14 at 22:48
  • You will need to understand how `CellEditor`s work and what `jbut.addActionListener(mactl);` is actually doing...Take a look at [Concepts: Editors and Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) – MadProgrammer Dec 02 '14 at 23:16

0 Answers0