我正在尝试构建jTable,其中我有一个很重的(不是很重)绘制jTable的每个单元格的任务。但我不是经常画画(很少画画/更新每个单元格)。实现jTable后我遇到了java.lang.OutOfMemoryError: Java heap space
。我发现这是由于每微秒调用paint(Graphics g, JComponent c)
。我不希望只在表被更新/修改的情况下一直调用此方法。有没有办法解决这个问题?
修改
我没有手动调用油漆。该表具有手动创建的UI,该UI使用setUI
方法设置。我已经使用这个UI创建了可以跨越多行或多列的单元格(即将几个单元格合并在一起)。
setUI(new MultiSpanCellTableUI());
类MultiSpanCellTableUI
实现了paint()
方法,该方法每秒都会调用一次。
public void paint(Graphics g, JComponent c) {
Rectangle oldClipBounds = g.getClipBounds();
Rectangle clipBounds = new Rectangle(oldClipBounds);
int tableWidth = table.getColumnModel().getTotalColumnWidth();
clipBounds.width = Math.min(clipBounds.width, tableWidth);
g.setClip(clipBounds);
int firstIndex = table.rowAtPoint(new Point(0, clipBounds.y));
int lastIndex = table.getRowCount() - 1;
Rectangle rowRect = new Rectangle(0, 0, tableWidth,
table.getRowHeight() + table.getRowMargin());
rowRect.y = firstIndex * rowRect.height;
for (int index = firstIndex; index <= lastIndex; index++) {
if (rowRect.intersects(clipBounds)) {
paintRow(g, index);
}
rowRect.y += rowRect.height;
}
g.setClip(oldClipBounds);
}
private void paintRow(Graphics g, int row) {
System.out.println("paintRow called");
Rectangle rect = g.getClipBounds();
boolean drawn = false;
AttributiveCellTableModel tableModel = (AttributiveCellTableModel) table
.getModel();
CellSpan cellAtt = (CellSpan) tableModel.getCellAttribute();
int numColumns = table.getColumnCount();
for (int column = 0; column < numColumns; column++) {
Rectangle cellRect = table.getCellRect(row, column, true);
int cellRow, cellColumn;
if (cellAtt.isVisible(row, column)) {
cellRow = row;
cellColumn = column;
} else {
cellRow = row + cellAtt.getSpan(row, column)[CellSpan.ROW];
cellColumn = column
+ cellAtt.getSpan(row, column)[CellSpan.COLUMN];
}
if (cellRect.intersects(rect)) {
drawn = true;
System.out.println("paintCell called!");
paintCell(g, cellRect, cellRow, cellColumn);
} else {
if (drawn)
break;
}
}
}
private void paintCell(Graphics g, Rectangle cellRect, int row, int column) {
int spacingHeight = table.getRowMargin();
int spacingWidth = table.getColumnModel().getColumnMargin();
Color c = g.getColor();
g.setColor(table.getGridColor());
g.drawRect(cellRect.x, cellRect.y, cellRect.width - 1,
cellRect.height - 1);
g.setColor(c);
cellRect.setBounds(cellRect.x + spacingWidth / 2, cellRect.y
+ spacingHeight / 2, cellRect.width - spacingWidth,
cellRect.height - spacingHeight);
if (table.isEditing() && table.getEditingRow() == row
&& table.getEditingColumn() == column) {
Component component = table.getEditorComponent();
component.setBounds(cellRect);
component.validate();
} else {
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(renderer, row, column);
if (component.getParent() == null) {
rendererPane.add(component);
}
rendererPane.paintComponent(g, component, table, cellRect.x,
cellRect.y, cellRect.width, cellRect.height, true);
}
}
由于它每隔一秒被调用一段时间后OutOfMemoryError
发生。我只需要在更新单元格中的内容时重新绘制单元格,并且可以轻松获取该信息。但是,如何根据该信息限制对paint()
的调用?
答案 0 :(得分:2)
不确定为什么需要
1)paint()
绘画,请JTable's Cel
l中的哪种类型的组件,如果有一些JComponent,你必须覆盖paintComponent()
而不是paint()
2)在较大的JTable
3)你必须看Renderer
,如果你使用一些颜色进行绘画,那么最好是prepareRenderer
4)JTable
默认返回JLabel
,在那里你可以设置Icon
instaead prerform一些自定义绘画
5)也许可以帮助您JTable's
表现Christmas Tree Applications
答案 1 :(得分:1)
如果上面没有发布代码,我认为您在paint()
组件上强制JTable
。
不需要在paint()
对象上调用JTable
。要自定义单元格绘制,请改用TableCellRenderer
。
public class MyCellRenderer extends JLabel implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
if (isSelected) {
// Is cell selected?
}
if (hasFocus) {
// Does this cell have the focus
}
// Configure the component with the specified value, here we are using JLabel
setText(value.toString());
// Set tool tip if desired
setToolTipText((String)value);
// Since the renderer is a component, return itself
return this;
}
}