getValueAt在鼠标悬停在单元格上调用

时间:2016-01-14 11:30:30

标签: java swing tablemodel

我注意到当我将光标移动到表的任何单元格上时,调用函数'getValueAt',向Eclipse控制台显示其输出。只是想知道为什么,不应该只在我创建tableModel对象时调用它?

我创建了一个类ModelloTabella,它实现了tableModel和 这是TableModel getValueAt实现:

@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();
    }

}

这是创建对象'ModelloTabella'的地方。我以为这个函数只在这里调用:

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

1 个答案:

答案 0 :(得分:2)

当表需要渲染单元格时(通常可能由于某些其他原因),JTable(通常)调用该方法,当您将鼠标悬停在单元格上时,实际上会触发工具提示事件{{{ 1}}首先获取单元格值然后将此值传递到JTable,然后CellRenderer检查相应的JTable以查看它是否具有Component值,如果它会显示,否则显示表toolTipText

toolTipText不会在内部缓存值,这就是模型的用途。因为相同的JTable用于呈现给定列的所有单元格,所以也无法缓存这些单元格的结果。因此,CellRenderer需要向模型询问一些信息

相关问题