我有一个Jtable,每行都有一个按钮来删除该行。 文本设置为"删除"。当我选择按钮时,文本消失。如何设置所选按钮的文本?
渲染器:
String s = "1-234-56s78===90";
System.out.println(s.chars().filter(i -> i >= '0' && i <= '9').distinct().count() == 10);
编辑:
public class JButtonRenderer extends JButton implements TableCellRenderer {
public JButtonRenderer(){
this.setText("Delete");
}
@Override
public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4,
int arg5) {
return this;
}
}
答案 0 :(得分:1)
尝试将此课程与TableCellEditor
,TableCellRenderer
class JButtonRenderer extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
public JButtonRenderer() {
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JButton b = new JButton("Delete");
return b;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, int column) {
JButton b = new JButton("Delete");
b.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// code
}
});
return b;
}
@Override
public Object getCellEditorValue() {
return null;
}
@Override
public boolean isCellEditable(EventObject anEvent) {
return true;
}
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
}
并使用:
JButtonRenderer compCellDelete = new JButtonRenderer();
jTable1.getColumnModel().getColumn(your_column_index).setCellEditor(compCellDelete);
jTable1.getColumnModel().getColumn(your_column_index).setCellRenderer(compCellDelete);