-1

I noticed that when i move the cursor over any cell of the table, the function 'getValueAt' gets called, showing into the Eclipse console its output. Was just wondering why, shouldn't it be called only when i create the tableModel object?

I created a class, ModelloTabella which implements tableModel and this is TableModel getValueAt implementation:

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Studente tmp=arrayStudenti.get(rowIndex);
    if(columnIndex==0){
        System.out.println("Stampo da getValueAt Matricola");
        return tmp.getMatricola();
    }else if(columnIndex==1){
        System.out.println("Stampo da getValueAt Nome");
        return tmp.getNome();
    }else{
        System.out.println("Stampo da getValueAt Cognome");
        return tmp.getCognome();
    }

}

This is where the object 'ModelloTabella' is created. I thought the function was called here only:

model = new ModelloTabella(this.controller.caricaStudenti(),controller);
            Tabella.setModel(model);

1 Answers1

2

The method is called by JTable (typically) whenever the table needs to render the cell (and for some other reasons probably), when you mouse over the cell, you actually trigger a tooltip event, the JTable first get's the cell value and then passes this value onto the CellRenderer, the JTable then checks the corresponding Component to see if it has a toolTipText value, if it does, it will be displayed, otherwise the tables toolTipText value is displayed

The JTable doesn't cache the values internally, that's what the model's used for. Because the same CellRenderer is used to render all the cells for a given column, it's also not possible to cache the results of those. So, the JTable needs to ask the model for some information

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366