1

I have a JTable with custom model and custom CellEditor and CellRenderer and I want to delete and add data in this table.

How do I do this?

My model:

public class frandtabmodule extends AbstractTableModel {
  Vector list;

  public frandtabmodule(Vector frand_list ) {
    this.list = frand_list;
  }


    @Override
  public Class getColumnClass(int columnIndex) { return Frand.class; }
    @Override
    public int getColumnCount() { return 1; }
    @Override
    public String getColumnName(int columnIndex) { return " "; }
    @Override
    public int getRowCount() { return (list == null) ? 0 : list.size(); }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        int i =0 ;
        System.out.println(" raw"+rowIndex+"   clumn"+columnIndex
                +"/n -------------------------------------------"+i++);
     return (list == null) ? null : list.get(rowIndex);


    }




    @Override
    public boolean isCellEditable(int columnIndex, int rowIndex) { return true; }


    @Override
    public void fireTableRowsDeleted(int firstRow, int lastRow) {

        this.list.remove(firstRow);
        this.fireTableDataChanged();
    }
}

My CellEditor and CellRenderer:

public class renderediter extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
  //Clientdef1 clientdef ;


  Frand list;

  public renderediter(){
   //clientdef = new Clientdef1();
  }

  /*private void updateData(Frand feed, boolean isSelected, JTable table) {
    this.feed = feed;

    clientdef.setconncted(feed.iscon);
    clientdef.name.setText(feed.name);
    if (isSelected) {
      clientdef.setBackground(table.getSelectionBackground());
    }else{
      clientdef.setBackground(table.getSelectionForeground());
    }
  }*/

    @Override
  public Component getTableCellEditorComponent(JTable table, Object value,
      boolean isSelected, int row, int column) {
   Frand frad = (Frand)value;

   System.err.println("-------------------------\n f1 called\n ------------------");
    //updateData(feed, true, table);

     frad.def.setBackground(table.getSelectionBackground());

    return frad.def;
  }

    @Override
  public Object getCellEditorValue() {
    return null;
  }

    @Override
  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
    Frand frad = (Frand)value;
   // updateData(feed, isSelected, table);
     if (isSelected) {
      frad.def.setBackground(table.getSelectionBackground());
    }
     else{
      frad.def.setBackground(Color.white);
    }
    return frad.def;
  }
}

Table creation:

contact_tab = new javax.swing.JTable();
this.module = new frandtabmodule(client.frands);
contact_tab.setModel(module);
contact_tab.setDefaultEditor(Frand.class, new renderediter());
contact_tab.setDefaultRenderer(Frand.class, new renderediter());
contact_tab.setRowHeight(45);
jsp_contact.add(contact_tab);
jsp_contact.repaint();
jsp_contact.revalidate();
contact_tab.setColumnSelectionAllowed(true);

What I've tried:

To add: client.frands.add(new Frand ());

To delete: client.frands.remove( frand);

This is working if I select Cell from table but I want to work dynamically.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zdi301
  • 55
  • 2
  • 9

1 Answers1

1

Don't override fireTableRowsDeleted() or related methods in AbstractTableModel. Instead, implement your own methods to addRow(), removeRow(), etc. Use DefaultTableModel as a guide; a related example is examined here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045