JTable渲染器慢?

时间:2013-04-19 11:30:28

标签: java performance swing jtable tablecellrenderer

当我使用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;
    }

}

with list =交易清单......

所以,我正在寻求帮助,我希望我能找到它:),我尝试了许多解决方案就像我使用面板列表而不是交易列表一样,jtable变得顺畅,但程序需要大量内存,我遇到内存泄漏与表&gt; 100行......

0 个答案:

没有答案