Reputation: 79
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) ;
}
}
Upvotes: 1
Views: 805
Reputation: 324137
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.
Upvotes: 1