1

当我使用 JTable 自定义渲染器和编辑器将 jpanel 添加到表格的单元格时,我注意到渲染变慢并且在上下滚动时 jtable 导航变得不可能......正如您将在下面看到的,我的渲染器类包含一个对象 TransactionUnit (jpanel),每次调用 getTableCellRendererComponent() 时,它都会将 Transaction 传递给 TransactionUnit 面板,该面板将使用该 Transaction 的数据填充字段并将自身返回给渲染器。

我的渲染器类:

package gui.transaction;

import baseclasses.Transaction;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;


public class RegistreRenderer implements TableCellRenderer{

TransactionUnit t;
public RegistreRenderer(){
    t = new TransactionUnit(null);
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean     isSelected, boolean hasFocus, int row, int column) {
    return t.getComponent((Transaction) value);
}

}

getComponent(Transaction t) 方法返回带有事务数据的 jpanel。

我的编辑类:

package gui.transaction;

import baseclasses.Transaction;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

public class RegistreEditor extends AbstractCellEditor implements TableCellEditor, ItemListener {

TransactionUnit t;
public RegistreEditor(){
t = new TransactionUnit(null);
}


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



@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    if(isSelected) {

        return t.getComponent((Transaction) value);
    }
    else {
        return null;
    }
}

}

我的表模型类:

public class Model extends AbstractTableModel{


    @Override
    public int getRowCount() {
       return list.size();
    }

    @Override
    public int getColumnCount() {
       return 1;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return list.get(rowIndex);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return TransactionUnit.class;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
       list.insertElementAt((Transaction) aValue, rowIndex);
       fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public String getColumnName(int column) {
        return "Registre";
    }

    public void supprimer(Transaction unit){
        list.remove(unit);
        fireTableDataChanged();
    }
    public void ajouter(Transaction unit){
        list.add(unit);
        fireTableDataChanged();
    }
    public void vider(){
        list.clear();
        fireTableDataChanged();
    }

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

    public void setList(Vector<Transaction> l){
        list = l;
    }

}

与列表 = 交易列表 ...

所以,我正在寻求帮助,我希望我能找到它:),我尝试了很多解决方案,就像我使用面板列表而不是事务列表时一样,jtable 变得流畅,但程序需要很多内存,我遇到超过 100 行的表的内存泄漏......

4

0 回答 0