我通过extendibg AbstractCellEditor类将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;
}
}
我做错了什么..我有一个Model类,它将列作为JButton并覆盖方法setValueAt和getValueAt。
答案 0 :(得分:2)
JTable使用渲染器显示数据。使用按钮作为编辑器单击单元格后,将按钮调用按钮编辑器一小段时间,然后将单元格放回渲染模式。因此,如果要更改文本,请更改模型中的值。