1

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 ?

Dan
  • 577
  • 1
  • 12
  • 38
  • 1
    [`JTable#getSelectedRow`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#getSelectedRow()), you'll want to pass it through [`JTable#convertRowIndexToModel`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#convertRowIndexToModel(int)) to get the model index. From there, it all comes down to the model, but [`DefaultTableModel#removeRow`](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html#removeRow(int)) should work – MadProgrammer Apr 10 '15 at 02:56
  • 2
    For what it's worth, putting a "delete" button on each row is just, well, 80's web. Better to use a combination of key bindings, tool bars/menus or buttons, as deleting 100 rows would just be tedious and the extra column could be used to display the data better. For [example](http://stackoverflow.com/questions/25070511/add-jbutton-to-each-row-of-a-jtable/25071138#25071138) and [example](http://stackoverflow.com/questions/24625083/how-to-delete-a-row-from-jtable/24626105#24626105) – MadProgrammer Apr 10 '15 at 02:57
  • Thanks you so much. Well I have a small table, so having button in each row with a customized look would solve the problem. The second link was my answer. I would choose it as answer if you submit it here again. However I did +1 for that post. – Dan Apr 10 '15 at 03:39
  • It's pretty much based on camickr's suggest anyway, so you might as well accept his answer as they do the same thing – MadProgrammer Apr 10 '15 at 03:42
  • 1
    @Dan, `The second link was my answer. I would choose it as answer..` - Can't tell if you are actually using my solution or not, but I would just point out that the "second link" does have one problem that I have noticed. Try doing a mousePressed on the first row and then move the mouse away from the column (to cancel the delete). Now move the mouse over a different row (say the last row) and do a mousePressed. The row is deleted even before the mouse is released. My solution avoid this unwanted delete since it uses a MouseListener to stop editing on the column when the mouse is released. – camickr Apr 10 '15 at 04:22
  • @camickr that was a good point actually. Thanks for mentioning it. – Dan Apr 10 '15 at 05:17

1 Answers1

2

Table Button Column shows an easy way to do this.

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

camickr
  • 321,443
  • 19
  • 166
  • 288