Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7050891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:11:56+00:00 2026-05-28T03:11:56+00:00

I try to implement a TableCellEditor that conains some fields and a delete-button. It

  • 0

I try to implement a TableCellEditor that conains some fields and a delete-button. It works good, but whe the row is deleted, the content in the deleted cell (rendered with TableCellEditor) is not updated.

I have tried to call both fireTableRowsDeleted(row, row) and fireTableDataChanged() in the model when the row is deleted, but it doesn’t seem to notify the TableCellEditor. It works when I select another row, and the row index is rendered with a TableCellRenderer again.

Any suggestions on how to notify the TableCellEditor on deletion?

  1. Delete button pressed

    enter image description here

  2. Row deleted, but CellEditor content not updated

    enter image description here

  3. Row content updated, when a CellRenderer is used again.

    enter image description here

Here is the code:

public class StringTableDemo extends JFrame {

    public StringTableDemo() {
        
        final StringTableModel model = new StringTableModel();
        model.addRow("Jonas");
        model.addRow("Hello");
        model.addRow("World");
        
        RendererAndEditor rendererAndEditor = new RendererAndEditor(model);
        
        JTable table = new JTable(model);
        table.setDefaultRenderer(Record.class, rendererAndEditor);
        table.setDefaultEditor(Record.class, rendererAndEditor);
        
        add(new JScrollPane(table), BorderLayout.CENTER);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class Record {
        String string;
        boolean isDeleted;
    }
    
    class StringTableModel extends AbstractTableModel {

        private final List<Record> data = new ArrayList<Record>();
        
        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public Object getValueAt(int row, int column) {
            return data.get(row);
        }
        
        @Override
        public Class<?> getColumnClass(int column) {
            return Record.class;
        }
        
        @Override
        public boolean isCellEditable(int row, int column) {
            return true;
        }
        
        @Override
        public void setValueAt(Object aValue, int row, int column) {
            if(aValue instanceof Record) {
                Record r = (Record)aValue;
                if(!r.isDeleted) {
                    data.set(row, r);
                    fireTableRowsUpdated(row, column);
                }
            } else throw new IllegalStateException("aValue is not a Record");
        }
        
        public void addRow(String s) {
            Record r = new Record();
            r.string = s;
            r.isDeleted = false;
            data.add(r);
            fireTableRowsInserted(data.size()-1, data.size()-1);
        }
        
        public void removeRow(int row) {
            data.remove(row);
            //fireTableRowsDeleted(row, row);
            fireTableDataChanged();
            
            System.out.println("row " + row + " deleted");
        }

    }
    
    class CellPanel extends JPanel {
        private final Action removeAction = new AbstractAction("x") {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                model.removeRow(index);
                isDeleted = true;
            }
            
        };
        private final JButton removeBtn = new JButton(removeAction);
        private final JTextField field = new JTextField();
        private final StringTableModel model;
        private int index;
        private boolean isDeleted = false;
        public CellPanel(StringTableModel model) {
            super(new BorderLayout());
            this.model = model;
            add(field, BorderLayout.CENTER);
            add(removeBtn, BorderLayout.EAST);
        }
        
        public Record getRecord() {
            Record r = new Record();
            r.string = field.getText();
            r.isDeleted = isDeleted;
            return r;
        }
        
        public void setRecord(Record r, int index) {
            field.setText(r.string);
            this.index = index;
        }
    }
    
    class RendererAndEditor extends AbstractCellEditor implements
     TableCellEditor, TableCellRenderer {

        private final CellPanel renderer;
        private final CellPanel editor;
        
        public RendererAndEditor(StringTableModel model) {
            renderer = new CellPanel(model);
            editor = new CellPanel(model);
        }
        
        @Override
        public Object getCellEditorValue() {
            return editor.getRecord();
        }

        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, 
                       int row, int column) {
            
            renderer.setRecord((Record)value, row);
            return renderer;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, 
                Object value, boolean isSelected, int row, int column) {
            editor.setRecord((Record)value, row);
            return editor;
        }
        
    }

    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new StringTableDemo();
            }
            
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:11:56+00:00Added an answer on May 28, 2026 at 3:11 am

    That is because your CellEditor doesn’t notice that it has to stop editing the cell, when you click on your delete button.

    A simple solution would be to add another ActionListener to your CellEditor and call stopCellEditing() every time you click on it. This should work:

    public RendererAndEditor( StringTableModel model )
    {
      renderer = new CellPanel( model );
      editor = new CellPanel( model );
    
      editor.getRemoveBtn().addActionListener( new ActionListener()
      {
        @Override
        public void actionPerformed( ActionEvent e )
        {
          stopCellEditing();
        }
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to implement some number type and I hit the issue that mynum
I had try to implement the send me log feature into my apps but
I decided I should try to implement coroutines (I think that's how I should
I am looking to try and implement a project management system for myself that
I try to implement callback interface with COM techonlogy and have IDL like that:
I'd like to try and implement some HCI for my existing OpenGL application. If
My boss for some reason wants to try to implement A/B Testing (or Split
I try to implement simple touch event handling on Blackberry 9550 emulator, but it
I try to implement the abstract DbCommand class (like OdbcCommand, OleDbCommand, ...) but a
I try to implement a table function by following Tony Andrews example . But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.