1
我正在通過extendibg AbstractCellEditor類將Jbutton添加到表中。但在按一下按鈕,從「開始」文本並沒有改變「停止」。這裏是我實現的類在表中添加JButton的問題
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;
}
}
什麼我做錯了。我有一個模型類需要該列作爲JButton並覆蓋了方法setValueAt和getValueAt。
我現在在模型中設置字符串值而不是按鈕,並使用該值渲染按鈕標題以及actionPerformed()方法。 – harshit 2010-08-02 15:45:53