当前位置: 技术问答>java相关
jTable中不同的行显示不同的颜色问题。200分请教,马上结。
来源: 互联网 发布时间:2015-11-08
本文导语: 在jTable中要求不同的行显示不同的颜色问题。 比如单行红色,双行白色。 有代码更好。 | Re: Different colors for Row in a JTable Author: paullarkin In Reply To: Different colors for Row in a JTable ...
在jTable中要求不同的行显示不同的颜色问题。
比如单行红色,双行白色。
有代码更好。
比如单行红色,双行白色。
有代码更好。
|
Re: Different colors for Row in a JTable
Author: paullarkin
In Reply To: Different colors for Row in a JTable
Feb 27, 2002 2:28 AM ?
Reply 2 of 13
Bit messy but you get the idea...
JTable table = new JTable(5,5); //example table
TableCellRenderer greyWhiteRenderer = new TableCellRenderer()
{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col)
{
JLabel label = new JLabel(); //for the normal behaviour
label.setOpaque(true);
if(row %2 != 0) //if it's odd
label.setBackground(Color.lightGray);
else
label.setBackground(Color.white);
//normal stuff
String text = (value == null) ? "" : value.toString();
label.setText(text);
if(isSelected)
label.setBackground(Color.cyan);
return label;
}
};
Enumeration columns = table.getColumnModel().getColumns();
while(columns.hasMoreElements()) //set the renderer for each column
((TableColumn)columns.nextElement()).setCellRenderer(greyWhiteRenderer);
Author: paullarkin
In Reply To: Different colors for Row in a JTable
Feb 27, 2002 2:28 AM ?
Reply 2 of 13
Bit messy but you get the idea...
JTable table = new JTable(5,5); //example table
TableCellRenderer greyWhiteRenderer = new TableCellRenderer()
{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col)
{
JLabel label = new JLabel(); //for the normal behaviour
label.setOpaque(true);
if(row %2 != 0) //if it's odd
label.setBackground(Color.lightGray);
else
label.setBackground(Color.white);
//normal stuff
String text = (value == null) ? "" : value.toString();
label.setText(text);
if(isSelected)
label.setBackground(Color.cyan);
return label;
}
};
Enumeration columns = table.getColumnModel().getColumns();
while(columns.hasMoreElements()) //set the renderer for each column
((TableColumn)columns.nextElement()).setCellRenderer(greyWhiteRenderer);
|
http://www2.gol.com/users/tame/swing/examples/JTableExamples4.html
|
up
|
up's up
|
up