Delete jTable row with jButton using cellEditor();

时间:2019-01-18 18:50:32

标签: java jtable jbutton

I want to add jButton inside column and then delete row using added jButton I've tried some stackoverflow questions and have following code but that doesn't deleting a row.

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;
            }

1 个答案:

答案 0 :(得分:0)

我们可以像下面的示例程序中那样进行操作。我们可以编写一个自定义渲染器+编辑器,它仅返回一个JButton作为渲染器/编辑器组件。请注意,行索引存储在编辑器的字段中。

import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.table.*;
import java.awt.Component;
import java.awt.event.*;
import java.util.EventObject;

public class ButtonInTable {

  public static void main(String[] args) {

    DefaultTableModel tableModel = new DefaultTableModel(
        new Object[][] {
            {"aa", "aaa", ""},
            {"bb", "bbb", ""},
            {"cc", "ccc", ""},
            {"dd", "ddd", ""},
            {"ee", "eee", ""}},
        new Object[] {"Column 1", "Column 2", " "});
    JTable table = new JTable(tableModel);
    table.getColumn(" ").setCellRenderer(new RendererAndEditor(table));
    table.getColumn(" ").setCellEditor(new RendererAndEditor(table));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(table));
    f.setBounds(300, 200, 400, 300);
    f.setVisible(true);
  }
}

class RendererAndEditor implements TableCellRenderer, TableCellEditor {

  private JButton button;
  private int row;

  RendererAndEditor(JTable table) {
    button = new JButton("Delete");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.removeRow(row);
      }
    });
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                 boolean hasFocus, int row, int column) {
    return button;
  }

  @Override
  public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
                                                        int column) {
    this.row = row;
    return button;
  }

  @Override
  public Object getCellEditorValue() {
    return null;
  }

  @Override
  public boolean isCellEditable(EventObject anEvent) {
    return true;
  }

  @Override
  public boolean shouldSelectCell(EventObject anEvent) {
    return true;
  }

  @Override
  public boolean stopCellEditing() {
    return true;
  }

  @Override
  public void cancelCellEditing() { }

  @Override
  public void addCellEditorListener(CellEditorListener l) { }

  @Override
  public void removeCellEditorListener(CellEditorListener l) { }
}