我想在列内添加 jButton,然后使用添加的 jButton 删除行我已经尝试了一些 stackoverflow 问题并具有以下代码,但这并没有删除一行。
public class NewJFrame extends javax.swing.JFrame {
JButton jb = new JButton();
public NewJFrame() {
initComponents();
jTable.getColumn(" ").setCellRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
jb.setText("Delete");
return jb;
}
});
jTable.getColumn(" ").setCellEditor(new TableCellEditor() {
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (isSelected) {
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTable = (JTable) e.getSource();
int modelRow = Integer.valueOf(e.getActionCommand());
((DefaultTableModel) jTable.getModel()).removeRow(modelRow);
}
});
}
return jb;
}
慕虎7371278
相关分类