Search code examples
javaswingjtableactionlistenerdefaulttablemodel

How to find the selected row in the JTable to remove it?


I tried to make a JTable that has one column as JButton for removing the selected row. But I still don't know what to add in the buttons ActionListener to identify its row and remove it.

Here is my code:

public class JavaApplication81 {
    JFrame frame;
    JPanel panel;
    JTable table;
    JScrollPane tableScroll = new JScrollPane();
    DefaultTableModel tableModel;
    public JavaApplication81(){
        frame = new JFrame("Frame");
        panel = new JPanel();

        String col[] = {" ", "File", "Remove"};
        tableModel = new DefaultTableModel(col,0);
        table = new JTable(){
            private static final long serialVersionUID = 1L;
            //Returning the Class of each column will allow different
            //renderes to be used based on class
            @Override
            public Class getColumnClass(int column){
                return getValueAt(0, column).getClass();
            }
        };
        table.setModel(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(400,200));
        tableScroll.setViewportView(table);

        Object[] data = {"icon", "file", "Remove"};
        tableModel.addRow(data);

        table.getColumn("Remove").setCellRenderer(new ButtonRenderer());
        table.getColumn("Remove").setCellEditor(new ButtonEditor(new JCheckBox()));

        panel.add(tableScroll);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(450, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        new JavaApplication81();
    }

    ///////////////////////
    public class ButtonRenderer extends JButton implements TableCellRenderer {

      public ButtonRenderer() {
        setOpaque(true);
      }

      public Component getTableCellRendererComponent(JTable table, Object value,
                       boolean isSelected, boolean hasFocus, int row, int column) {

        setText("Remove");
        return this;
      }
    }

    public class ButtonEditor extends DefaultCellEditor {
        protected JButton button;

        public ButtonEditor(JCheckBox checkBox) {
          super(checkBox);
          button = new JButton();
          button.setOpaque(true);
          button.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {

            }
          });
        }

        public Component getTableCellEditorComponent(JTable table, Object value,
                         boolean isSelected, int row, int column) {


          button.setText("Remove");
          return button;
        }
    }
}

Any idea to remove the row of each button when it is pressed ?


Solution

  • Table Button Column shows an easy way to do this.

    It provides the renderer/editor and a simple way to get the row.