1

I'm devloping a application with Java swing ,my problem is : if i'm adding a new row , the getTableCellRendererComponent not called , my code :

the create of table :

   public Tablecase() {

       SystemeBaseConnaissance = new LESSymptomesEnsembleEt();
       SystemeBaseConnaissance.lesSymptomesEnsembleEt.add("x");
       initComponents();
       jTable1.setTableHeader(null);
       jTable1.getColumn("Title 1").setCellRenderer(new brmcellrender());

       Model model = new Model();

       jTable1.setModel( model);
       model.addRow(new Object[]{"ss"});
       System.out.println(SystemeBaseConnaissance.lesSymptomesEnsembleEt.size());
    }

my Defaulttablemodel :

 class Model extends DefaultTableModel{

    public Model(){

        super();
     this.addRow(new Object[]{"sx"}  );    
       repaint();

    }

    @Override
    public void addRow(Object[] rowData) {
        super.addRow(rowData); //To change body of generated methods, choose Tools | Templates.
    }


}

my DefaultCellEditor :

    public class brmcelleditor extends DefaultCellEditor{

    public brmcelleditor(JTextField textField) {
        super(textField);
    }
    }


     public class brmcellrender extends DefaultTableCellRenderer{

    public brmcellrender() {
        super();
        System.out.println("mefgoudabrahim20.Tablecase.brmcellrender.<init>()");
    /**/
    }

    /*affichge */

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            System.out.println("mefgoudabrahim20.Tablecase.brmcellrender.<init>qsdqsdqsd()");
            CaseOfTable cas = new CaseOfTable() ; 
            return  ( cas) ;
        }



    }
MohaMed
  • 81
  • 1
  • 2
  • 10
  • 1
    A complete example using `setCellRenderer()` is examined [here](http://stackoverflow.com/q/17157915/230513). – trashgod Mar 28 '16 at 15:44
  • my problem in setCellRenderer() ? – MohaMed Mar 28 '16 at 15:50
  • If it's _not_ set, either by column or class, the default renderer will be used. – trashgod Mar 28 '16 at 15:52
  • not , it's set !!!!!!!!!! – MohaMed Mar 28 '16 at 16:00
  • 2
    please whats reason for repaint() in DefaultTableModel, better should be search here Java Swing JTable DefaultTableModel, lots of Q&A in SSCCE / MCVE form, follows this idea (code in SSCCE / MCVE form) in future, for me this question isn't answerable – mKorbel Mar 28 '16 at 16:32
  • 2
    Assuming that the [example cited](http://stackoverflow.com/q/17157915/230513) works for you, please edit your question to include a [mcve] that shows your current approach. – trashgod Mar 28 '16 at 16:33

1 Answers1

1

if i'm adding a new row , the getTableCellRendererComponent not called

The problem is not because you add a new row.

jTable1.getColumn("Title 1").setCellRenderer(new brmcellrender());
Model model = new Model();
jTable1.setModel( model);

The problem is that you set the model after you set the renderer. When you set the model of a table the TableColumnModel and all the TableColums are recreated which means you lose the custom renderers you added to the TableColumn.

The code should be:

Model model = new Model();
jTable1.setModel( model);
jTable1.getColumn("Title 1").setCellRenderer(new brmcellrender());

Also, you would not create a new model every time you add a row. The point of adding a row is to add it to the existing model.

Finally, class names SHOULD start with an upper case character. Fix your renderer name.

camickr
  • 321,443
  • 19
  • 166
  • 288