Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. JTable cell not going away! SOLVED

JTable cell not going away! SOLVED

Scheduled Pinned Locked Moved Java
comjsonhelpquestionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    venomation
    wrote on last edited by
    #1

    SOLUTION: This works for me but I still do not know why it was behaving like that...

    if (table.getCellEditor() != null) {
    table.getCellEditor().stopCellEditing();
    }

    Was called when deleting the row, forcing the cell that is being edited to stop editing. PROBLEM: I have the following CellEditor that is used to validate cells on a column:

    public class CellEditor extends AbstractCellEditor implements TableCellEditor {

    protected TextField source = new TextField();
    private ArrayList validators = new ArrayList();
    
    public CellEditor WithValidation(IValidator validator) {
        validators.add(validator);
        return this;
    }
    
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    
        source.setText(String.valueOf(value));
        return source;
    }
    
    public Object getCellEditorValue() {
        return source.getText();
    }
    
    @Override
    public boolean stopCellEditing() {
        String s = ((String) this.getCellEditorValue());
        if (s == null) {
            this.fireEditingCanceled();
            return false;
        }
    
        for (IValidator v : this.validators) {
            if (!v.isValid(s)) {
                JOptionPane.showMessageDialog(null, v.getErrorMessage());
                this.fireEditingCanceled();
                return false;
            }
        }
        return super.stopCellEditing();
    }
    

    }

    And I attach the code to my table like this:

    this.jTable1.getColumnModel()
    .getColumn(0)
    .setCellEditor(new loyaltyapp.validation.CellEditor()
    .WithValidation(new TextLengthValidator(1, 12)));

    When the second snippet of code is commented everything works as intended (but of course not including any validation)... The problem is that when I do attach the CellEditor to my table when I remove a row WHILE a cell is being edited the rest of the row gets deleted, leaving behind a rogue cell! Image of selecting a cell: http://www.mediafire.com/?8n999a4djacdj84[^] Image of deleting the row while selecting the validation cell: http://www.mediafire.com/?mozjjzc53ju12b2[<

    T 1 Reply Last reply
    0
    • V venomation

      SOLUTION: This works for me but I still do not know why it was behaving like that...

      if (table.getCellEditor() != null) {
      table.getCellEditor().stopCellEditing();
      }

      Was called when deleting the row, forcing the cell that is being edited to stop editing. PROBLEM: I have the following CellEditor that is used to validate cells on a column:

      public class CellEditor extends AbstractCellEditor implements TableCellEditor {

      protected TextField source = new TextField();
      private ArrayList validators = new ArrayList();
      
      public CellEditor WithValidation(IValidator validator) {
          validators.add(validator);
          return this;
      }
      
      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
      
          source.setText(String.valueOf(value));
          return source;
      }
      
      public Object getCellEditorValue() {
          return source.getText();
      }
      
      @Override
      public boolean stopCellEditing() {
          String s = ((String) this.getCellEditorValue());
          if (s == null) {
              this.fireEditingCanceled();
              return false;
          }
      
          for (IValidator v : this.validators) {
              if (!v.isValid(s)) {
                  JOptionPane.showMessageDialog(null, v.getErrorMessage());
                  this.fireEditingCanceled();
                  return false;
              }
          }
          return super.stopCellEditing();
      }
      

      }

      And I attach the code to my table like this:

      this.jTable1.getColumnModel()
      .getColumn(0)
      .setCellEditor(new loyaltyapp.validation.CellEditor()
      .WithValidation(new TextLengthValidator(1, 12)));

      When the second snippet of code is commented everything works as intended (but of course not including any validation)... The problem is that when I do attach the CellEditor to my table when I remove a row WHILE a cell is being edited the rest of the row gets deleted, leaving behind a rogue cell! Image of selecting a cell: http://www.mediafire.com/?8n999a4djacdj84[^] Image of deleting the row while selecting the validation cell: http://www.mediafire.com/?mozjjzc53ju12b2[<

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #2

      The problem is that when I do attach the CellEditor to my table when I remove a row WHILE a cell is being edited the rest of the row gets deleted, leaving behind a rogue cell! Then decide - Editing OR deleting. Can't have both. Is the system deleting the row or how is it done?

      regards Torsten When I'm not working

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups