对奇数行和偶数行使用不同的背景颜色是提高大型表格可读性的常用技巧。
我想在 Swing 的 JTable 中使用这个效果。我首先创建了一个自定义表格渲染器,但这只能用于绘制实际的单元格,而且我还想将条纹添加到表格的“白色”部分,那里可能没有单元格。我可以子类化 JTable 并重写 paintComponent(),但我更喜欢可以只更改表格呈现的选项。
有更好的方法吗?
编辑:根据目前的答案,如果不扩展 JTable,这似乎是不可能的。但是,当我覆盖 JTable.paintComponent() 时,它也仅绘制有行的区域。剩下的怎么画?
最佳答案
使用 getCellRect( getRowCount() - 1, 0, true ).y
获取空白区域的顶部 y 坐标,然后用 paintComponent( Graphics g )
.
为了让您更轻松,这里有一个很长(但完整)的解决方案;-)
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class StripedEvenInWhitePartsTable extends JTable
{
public StripedEvenInWhitePartsTable( String[][] data, String[] fields )
{
super( data, fields );
setFillsViewportHeight( true ); //to show the empty space of the table
}
@Override
public void paintComponent( Graphics g )
{
super.paintComponent( g );
paintEmptyRows( g );
}
public void paintEmptyRows( Graphics g )
{
Graphics newGraphics = g.create();
newGraphics.setColor( UIManager.getColor( "Table.gridColor" ) );
Rectangle rectOfLastRow = getCellRect( getRowCount() - 1, 0, true );
int firstNonExistentRowY = rectOfLastRow.y; //the top Y-coordinate of the first empty tablerow
if ( getVisibleRect().height > firstNonExistentRowY ) //only paint the grid if empty space is visible
{
//fill the rows alternating and paint the row-lines:
int rowYToDraw = (firstNonExistentRowY - 1) + getRowHeight(); //minus 1 otherwise the first empty row is one pixel to high
int actualRow = getRowCount() - 1; //to continue the stripes from the area with table-data
while ( rowYToDraw < getHeight() )
{
if ( actualRow % 2 == 0 )
{
newGraphics.setColor( Color.ORANGE ); //change this to another color (Color.YELLOW, anyone?) to show that only the free space is painted
newGraphics.fillRect( 0, rowYToDraw, getWidth(), getRowHeight() );
newGraphics.setColor( UIManager.getColor( "Table.gridColor" ) );
}
newGraphics.drawLine( 0, rowYToDraw, getWidth(), rowYToDraw );
rowYToDraw += getRowHeight();
actualRow++;
}
//paint the column-lines:
int x = 0;
for ( int i = 0; i < getColumnCount(); i++ )
{
TableColumn column = getColumnModel().getColumn( i );
x += column.getWidth(); //add the column width to the x-coordinate
newGraphics.drawLine( x - 1, firstNonExistentRowY, x - 1, getHeight() );
}
newGraphics.dispose();
} //if empty space is visible
} //paintEmptyRows
public Component prepareRenderer( TableCellRenderer renderer, int row, int column )
{
Component c = super.prepareRenderer( renderer, row, column );
if ( !isRowSelected( row ) )
{
c.setBackground( row % 2 == 0 ? getBackground() : Color.ORANGE );
}
return c;
}
public static void main( String[] argv )
{
String data[][] = { { "A0", "B0", "C0" }, { "A1", "B1", "C1" }, { "A2", "B2", "C2" }, { "A3", "B3", "C3" }, { "A4", "B4", "C4" } };
String fields[] = { "A", "B", "C" };
JFrame frame = new JFrame( "a JTable with striped empty space" );
StripedEvenInWhitePartsTable table = new StripedEvenInWhitePartsTable( data, fields );
JScrollPane pane = new JScrollPane( table );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( pane );
frame.setSize( 400, 300 );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
这个例子可以扩展为:
- 修复变量 RowHeights 的绘制伪网格(我使用的是任何行中使用的最低高度)
- 向用户解释为什么当他点击空白处编辑单元格时没有任何反应(通过工具提示)
- 如果用户在空白处单击,则向表格模型添加一个额外的行(不要!不要使用 Excel!)
- 使用空白区域绘制表格的倒影(包括所有呈现的数据(有什么用?;-))
关于java - 带条纹背景的 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3184975/