简体   繁体   中英

Deletion of JTable row with custom cell editor

I have a JTable in which I have added a JButton(Delete button) and a JComboBox as custom cell Editors on two columns. Now, when I select an item in Combobox and click on Delete button, the selected row gets deleted but the combobox of the deleted row is rendered on the row. Do I have to remove the cell editor as well from the table. The code works fine if I dont select my combobox. Please help me.

This is how my code looks like:-

class JTableRenderer extends JButton implements TableCellRenderer, TableCellEditor{

    private JComboBox   sens_type_cb;

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


     if(column == 1)
        {
            System.out.println("Row " + row);

            sens_type_cb = new JComboBox(new String[]{"Radar", "EO", "Aerial", "UAV", "Other"});
            return sens_type_cb;

        }

    }

     @Override
    public void addActionListener(ActionListener listener)
    {

        sens_type_cb.addActionListener(listener);

    }


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


        if(column == 1)
        {
           ActionListener listen = new ActionListener() 
            {
                @Override
                public void actionPerformed(ActionEvent e) 
                {

                        if (table.isEditing())
                            table.getCellEditor().stopCellEditing();
                        //table.clearSelection();

                }
            };
            sens_type_cb.addActionListener(listen);
            return sens_type_cb;

        }


        else
            return super.getParent(); 

    }

public void addActionListener(ActionListener listener)
    {

        sens_type_cb.addActionListener(listener);

    }


Next I create Class SensorConf in which I create a table:-

public class SensorConf extends javax.swing.JFrame {

    private static SensorConf conf_obj = new SensorConf();
    /* A private Constructor prevents any other class from instantiating. */
    //private SensorConf(){ }
    /* Static 'instance' method */
    public static SensorConf getInstance( ) {
        return conf_obj;
    }

    Action delete = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        JTable table = (JTable)e.getSource();
        int modelRow = Integer.valueOf( e.getActionCommand() );
        ((DefaultTableModel)table.getModel()).removeRow(modelRow);
    }
};
    public void CreateTable()
    {


       //  jTable1.setModel(model);
         jTable1.getColumn("S.No.").setPreferredWidth(40);
         jTable1.getColumn("Sensor Type").setPreferredWidth(80);

         jTable1.getColumn("").setPreferredWidth(40);
         jTable1.getColumnModel().getColumn(1).setResizable(false);
         jTable1.getTableHeader().setReorderingAllowed(false);
         jTable1.setRowHeight(25);



         javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel)jTable1.getModel();
         model.addRow(new Object[]{"", "", "", "", "", ""});

         JTableRenderer tableRenderer = new JTableRenderer();


         model.getRowCount();
         model.setValueAt(new Integer(model.getRowCount()), model.getRowCount()-1,0);


         jTable1.getColumnModel().getColumn(1).setCellEditor(tableRenderer);
         jTable1.getColumnModel().getColumn(1).setCellRenderer(tableRenderer);


         ButtonColumn buttonColumn = new ButtonColumn(jTable1, delete, 6);

       }

        private SensorConf() {
        initComponents();
        CreateTable();

    }

I instantiate an object of my class in main and on clicking an Add Button outside the table, I add a row model.addRow(new Object[]{"", "", "", "", "", ""}); to my table. Please help me as to find out where am I committing a mistake. Thanks.

Sounds like the comboBox cell is still in editing mode. You would need to make sure editing on the cell has been cancelled before you delete the row.

You can try using Table Button Column . I must admit I've never tried this class in your scenario, but the code does invoke fireEditingStopped() , so expect it should work.

Edit:

You may find Table Stop Editing useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM