0

I've implemented a customized table with cell renderer so that I can add the delete button on every row in the table


ButtonEditor.java

public class ButtonEditor extends DefaultCellEditor{
    protected TableButton btn;
    private String lbl;
    private Boolean clicked;

    public ButtonEditor(JTextField txt) {
        super(txt);

        btn= new TableButton();
        btn.setOpaque(true);

        //WHEN BUTTON IS CLICKED
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                fireEditingStopped();
                JOptionPane.showMessageDialog(btn, lbl+" Clicked");
            }
        });
    }

   //OVERRIDE A COUPLE OF METHODS
    @Override
    public Component getTableCellEditorComponent(JTable table, Object obj,
        boolean selected, int row, int col) {

        //SET TEXT TO BUTTON,SET CLICKED TO TRUE,THEN RETURN THE BTN OBJECT
        lbl=(obj==null) ? "":obj.toString();
        btn.setText(lbl);
        clicked=true;
        return btn;
    }

    //IF BUTTON CELL VALUE CHNAGES,IF CLICKED THAT IS
    @Override
    public Object getCellEditorValue() {
        if(clicked)
        {
            JOptionPane.showMessageDialog(btn, lbl+" Clicked");
        }
        //SET IT TO FALSE NOW THAT ITS CLICKED
        clicked=false;
        return new String(lbl);
    }

    @Override
    public boolean stopCellEditing() {

         //SET CLICKED TO FALSE FIRST
        clicked=false;
        return super.stopCellEditing();
    }

    @Override
    protected void fireEditingStopped() {
        // TODO Auto-generated method stub
        super.fireEditingStopped();
    }
}

ButtonRenderer.java

public class ButtonRenderer extends JButton implements  TableCellRenderer
{
    //CONSTRUCTOR
    public ButtonRenderer() {
        //SET BUTTON PROPERTIES
        setOpaque(true);
    }
    @Override
    public Component getTableCellRendererComponent(JTable table, Object obj,
        boolean selected, boolean focused, int row, int col) {

        //SET PASSED OBJECT AS BUTTON TEXT
        setText((obj==null) ? "":obj.toString());

        return this;
    }
}

Inside the JPanel constructor I need to add a row of that added table palette in swing.

public samplePanel(){
    table.addRow(new Object[] {o.get(0), o.get(1), o.get(2), o.get(3), o.get(4), "DELETE"});
    table.getColumn("Action").setCellRenderer(new ButtonRenderer());
    table.getColumn("Action").setCellEditor(new ButtonEditor(new JTextField()));
}

Reference for the button editor and renderer click here.

I just wanted to get the data of the row clicked delete button, But I don't know how to do it since I am new to renderers and editors.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I'm just going to point to this [example](https://stackoverflow.com/questions/25070511/add-jbutton-to-each-row-of-a-jtable/25071138#25071138) – MadProgrammer Mar 31 '22 at 08:57
  • But that's too complex. – john michael ringor Mar 31 '22 at 08:59
  • If you're still hell bent on making your users lives miserable (), you can look at [this example](https://stackoverflow.com/questions/12795442/how-can-remove-current-row-in-jtable-when-click-jbutton/12795907#12795907); [this example](https://stackoverflow.com/questions/24625083/how-to-delete-a-row-from-jtable/24626105#24626105); [this example](https://stackoverflow.com/questions/51841524/remove-a-row-from-jtable-with-jbutton/51842086#51842086) – MadProgrammer Mar 31 '22 at 09:00
  • If you think (the first example) is *"to complex"* then you are in for a seriously rude awaken – MadProgrammer Mar 31 '22 at 09:01
  • Check out [Table Button Column](https://tips4java.wordpress.com/2009/07/12/table-button-column/) for an easy way to add a button to a column along with an Action to be invoked when the button is clicked. – camickr Mar 31 '22 at 13:37

0 Answers0