tags:

views:

16

answers:

1

I am adding Jbutton to a table by extendibg AbstractCellEditor class. But on click of button the text doesnt change from "Start" to "Stop" .here is the class i implemented

     public class ButtonEditor1 extends AbstractCellEditor implements     
      TableCellEditor,ActionListener,TableCellRenderer{

       JButton btnSTART =  new JButton("START");
        private JTable table ;

     public ButtonEditor1(JTable table){
    this.table = table;
    btnSTART.addActionListener(this);
    }
public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {

    return btnSTART;
}

public Object getCellEditorValue() {
    // TODO Auto-generated method stub

    return btnSTART;
}

public void actionPerformed(ActionEvent e) {
    int row = table.getSelectedRow();
    if(btnSTART.getText().equals("START")){
        if(row != -1){
            btnSTART.setText("STOP");
        }
    }else if(btnSTART.getText().equals("STOP")){

        if(row != -1){
            btnSTART.setText("START");
        }
    }
    fireEditingStopped();
}
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    return btnSTART;
}

}

what i am doing wrong .. I have a Model class which takes the column as JButton and have overriden the method setValueAt and getValueAt.

+2  A: 

A JTable uses renderers to display data. Once you click on the cell using the button as an editor, the button editor is invoked for a split second, then the cell is placed back in rendering mode. So if you want to change the text you change the value in the model.

camickr
i am now setting the String value in the model instead of button and renderer the button title as well the actionPerformed() method with that value.
harshit