我自己写了一个TestjTable类继承自JTable,写了一个TestBasicTableUI继承自BasicTableUI,在TestBasicTableUI重写了paint、paintRow和paintCell,在TestjTable类中使用setUI(new TestBasicTableUI())。new一个TestjTable tt = new TestjTable(10,10),使用tt.setRowHeight(1, 50)设置第2行的高度时,该方法不起作用。请高手指点一下,谢谢。
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;public class TestBasicTableUI extends BasicTableUI {
public void paint(Graphics g, JComponent c) {
System.out.println("call paint match");
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) {
Rectangle rect = g.getClipBounds();
boolean drawn = false;
System.out.println("call paintRow match"+row);
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;
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();
System.out.println("call paintCell match");
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);
}
}
}
package testtable;import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;
import javax.swing.event.*;import TestResizable.TableColumnResizer;
import TestResizable.TableRowResizer;public class TestjTable extends JTable {
protected int rowHeight = 17;
private static int i = 0;
protected MouseInputAdapter rowResizer = null;
private SizeSequence rowModel;
public TestjTable(TableModel model) {
super(model);
setUI(new TestBasicTableUI());
getTableHeader().setReorderingAllowed(false);
setCellSelectionEnabled(true);
setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
} public Rectangle getCellRect(int row, int column, boolean includeSpacing) {
Rectangle sRect = super.getCellRect(row, column, includeSpacing);
if ((row < 0) || (column < 0) || (getRowCount() <= row)
|| (getColumnCount() <= column)) {
return sRect;
}
CellSpan cellAtt = (CellSpan) ((AttributiveCellTableModel) getModel())
.getCellAttribute();
if (!cellAtt.isVisible(row, column)) {
int temp_row = row;
int temp_column = column;
row += cellAtt.getSpan(temp_row, temp_column)[CellSpan.ROW];
column += cellAtt.getSpan(temp_row, temp_column)[CellSpan.COLUMN];
}
int[] n = cellAtt.getSpan(row, column); int index = 0;
int columnMargin = getColumnModel().getColumnMargin();
Rectangle cellFrame = new Rectangle();
int aCellHeight = rowHeight + rowMargin;
cellFrame.y = row * aCellHeight;
cellFrame.height = n[CellSpan.ROW] * aCellHeight; Enumeration enumeration = getColumnModel().getColumns();
while (enumeration.hasMoreElements()) {
TableColumn aColumn = (TableColumn) enumeration.nextElement();
cellFrame.width = aColumn.getWidth();// + columnMargin;
if (index == column)
break;
cellFrame.x += cellFrame.width;
index++;
}
for (int i = 0; i < n[CellSpan.COLUMN] - 1; i++) {
TableColumn aColumn = (TableColumn) enumeration.nextElement();
cellFrame.width += aColumn.getWidth();// + columnMargin;
} if (!includeSpacing) {
Dimension spacing = getIntercellSpacing();
cellFrame.setBounds(cellFrame.x + spacing.width / 2, cellFrame.y
+ spacing.height / 2, cellFrame.width - spacing.width,
cellFrame.height - spacing.height + 60);
}
return cellFrame;
} private int[] rowColumnAtPoint(Point point) {
int[] retValue = { -1, -1 };
int row = point.y / (rowHeight + rowMargin);
if ((row < 0) || (getRowCount() <= row))
return retValue;
int column = getColumnModel().getColumnIndexAtX(point.x); CellSpan cellAtt = (CellSpan) ((AttributiveCellTableModel) getModel())
.getCellAttribute(); if (cellAtt.isVisible(row, column)) {
retValue[CellSpan.COLUMN] = column;
retValue[CellSpan.ROW] = row;
return retValue;
}
retValue[CellSpan.COLUMN] = column
+ cellAtt.getSpan(row, column)[CellSpan.COLUMN];
retValue[CellSpan.ROW] = row
+ cellAtt.getSpan(row, column)[CellSpan.ROW];
return retValue;
} public int rowAtPoint(Point point) {
return rowColumnAtPoint(point)[CellSpan.ROW];
} public int columnAtPoint(Point point) {
return rowColumnAtPoint(point)[CellSpan.COLUMN];
} public void columnSelectionChanged(ListSelectionEvent e) {
repaint();
} public void valueChanged(ListSelectionEvent e) {
int firstIndex = e.getFirstIndex();
int lastIndex = e.getLastIndex();
if (firstIndex == -1 && lastIndex == -1) {
repaint();
}
Rectangle dirtyRegion = getCellRect(firstIndex, 0, false);
int numCoumns = getColumnCount();
int index = firstIndex;
for (int i = 0; i < numCoumns; i++) {
dirtyRegion.add(getCellRect(index, i, false));
}
index = lastIndex;
for (int i = 0; i < numCoumns; i++) {
dirtyRegion.add(getCellRect(index, i, false));
}
repaint(dirtyRegion.x, dirtyRegion.y, dirtyRegion.width,
dirtyRegion.height);
}
public void setResizable(boolean row, boolean column){
if(row){
if(rowResizer==null)
rowResizer = new TableRowResizer(this);
}else if(rowResizer!=null){
removeMouseListener(rowResizer);
removeMouseMotionListener(rowResizer);
rowResizer = null;
}
} public void changeSelection(int row, int column, boolean toggle, boolean extend) {
if(getCursor()==TableRowResizer.resizeCursor)
return;
super.changeSelection(row, column, toggle, extend);
} public void setRowHeight(int rowHeight) {
if (rowHeight <= 0) {
throw new IllegalArgumentException("New row height less than 1");
}
int old = this.rowHeight;
this.rowHeight = rowHeight;
rowModel = null;
resizeAndRepaint();
firePropertyChange("rowHeight", old, rowHeight);
} public int getRowHeight() {
return rowHeight;
} public void setRowHeight(int row, int rowHeight) {
if (rowHeight <= 0) {
throw new IllegalArgumentException("New row height less than 1");
}
getRowModel().setSize(row, rowHeight);
resizeAndRepaint();
} public int getRowHeight(int row) {
return (rowModel == null) ? getRowHeight() : rowModel.getSize(row);
} private SizeSequence getRowModel() {
if (rowModel == null) {
rowModel = new SizeSequence(getRowCount(), getRowHeight());
}
return rowModel;
}
protected void resizeAndRepaint() {
revalidate();
repaint();
}}