1 year ago

#358091

test-img

john michael ringor

How can I get the data of a table row on a click button in the same row as the table row with button renderer and cell rederer?

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.

java

swing

renderer

cellrenderer

0 Answers

Your Answer

Accepted video resources