sql: select field1, field3, (case when field1 is null then 'F' when field3 is null then 'T' else 'E' end) as comp from your_table在java端根据sql的结果循环往表格填数据(只显示field1和field3就可以了),然后判断comp的结果(F为first的意思,说明第一列为空,T为Third的意思,说明第三列为空,E为相等equals的意思),根据不同的结果把表格的行的背景色设置为你要的颜色。
谢谢大家热心回复! 颜色的控制可以了,之前控制不了颜色的原因是使用的 JTable.setDefaultRenderer(arg0, arg1);该方法没有什么反映,第一个参数不管是String.class还是Component.class,都是没有效果,完全不象java core 2 中的描述,后来是使用的m_tColumn.setCellRenderer(m_tColorRenderer) ; for(int i = 0 ; i < m_tListView.getColumnCount() ; i++){ TableColumn m_tColumn = m_tListView.getColumn(m_tListView .getColumnName(i)); m_tColumn.setCellRenderer(m_tColorRenderer) ; } 就可显示颜色了。 但是,又有了新的问题,设置字体的颜色难道就是设置前景色吗(setForeground)? *还有更重要的是使用下面两种ColorTableRowRenderer绘制的表格不能用选!!!那位能在帮帮忙!谢谢 1. class ColorTableRowRenderer extends JLable implements TableCellRenderer { public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) { if ((arg0.getValueAt(arg4, 0)).equals(arg0.getValueAt(arg4, 2))) { if ((arg0.getValueAt(arg4, 1)).equals(arg0.getValueAt(arg4, 3))) { setForeground(Color.RED); } else { setForeground(Color.red); } } else if ((arg0.getValueAt(arg4, 0)).equals("") && (!(arg0.getValueAt(arg4, 2).equals("")))) { setForeground(Color.BLUE); } else if ((arg0.getValueAt(arg4, 2)).equals("") && (!(arg0.getValueAt(arg4, 0).equals("")))) { setForeground(Color.ORANGE); } setText(arg1.toString()); setBackground(Color.WHITE); return this; }}2. class ColorTableRowRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) { if ((arg0.getValueAt(arg4, 0)).equals(arg0.getValueAt(arg4, 2))) { if ((arg0.getValueAt(arg4, 1)).equals(arg0.getValueAt(arg4, 3))) { setForeground(Color.RED); } else { setForeground(Color.red); } } else if ((arg0.getValueAt(arg4, 0)).equals("") && (!(arg0.getValueAt(arg4, 2).equals("")))) { setForeground(Color.BLUE); } else if ((arg0.getValueAt(arg4, 2)).equals("") && (!(arg0.getValueAt(arg4, 0).equals("")))) { setForeground(Color.ORANGE); } setText(arg1.toString()); setBackground(Color.WHITE); return this; }}
class ColorTableRowRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) { if ((arg0.getValueAt(arg4, 0)).equals(arg0.getValueAt(arg4, 2))) { if ((arg0.getValueAt(arg4, 1)).equals(arg0.getValueAt(arg4, 3))) { setForeground(Color.RED); } else { setForeground(Color.red); } } else if ((arg0.getValueAt(arg4, 0)).equals("") && (!(arg0.getValueAt(arg4, 2).equals("")))) { setForeground(Color.BLUE); } else if ((arg0.getValueAt(arg4, 2)).equals("") && (!(arg0.getValueAt(arg4, 0).equals("")))) { setForeground(Color.ORANGE); } else { setForeground(Color.BLACK); } setText(arg1.toString()); setBackground(Color.WHITE); return super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5); } }
select field1, field3, (case when field1 is null then 'F' when field3 is null then 'T' else 'E' end) as comp from your_table在java端根据sql的结果循环往表格填数据(只显示field1和field3就可以了),然后判断comp的结果(F为first的意思,说明第一列为空,T为Third的意思,说明第三列为空,E为相等equals的意思),根据不同的结果把表格的行的背景色设置为你要的颜色。
颜色的控制可以了,之前控制不了颜色的原因是使用的 JTable.setDefaultRenderer(arg0, arg1);该方法没有什么反映,第一个参数不管是String.class还是Component.class,都是没有效果,完全不象java core 2 中的描述,后来是使用的m_tColumn.setCellRenderer(m_tColorRenderer) ;
for(int i = 0 ; i < m_tListView.getColumnCount() ; i++){
TableColumn m_tColumn = m_tListView.getColumn(m_tListView
.getColumnName(i));
m_tColumn.setCellRenderer(m_tColorRenderer) ;
}
就可显示颜色了。 但是,又有了新的问题,设置字体的颜色难道就是设置前景色吗(setForeground)?
*还有更重要的是使用下面两种ColorTableRowRenderer绘制的表格不能用选!!!那位能在帮帮忙!谢谢
1. class ColorTableRowRenderer extends JLable implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable arg0, Object arg1,
boolean arg2, boolean arg3, int arg4, int arg5) {
if ((arg0.getValueAt(arg4, 0)).equals(arg0.getValueAt(arg4, 2))) {
if ((arg0.getValueAt(arg4, 1)).equals(arg0.getValueAt(arg4, 3))) {
setForeground(Color.RED);
} else {
setForeground(Color.red);
}
} else if ((arg0.getValueAt(arg4, 0)).equals("")
&& (!(arg0.getValueAt(arg4, 2).equals("")))) {
setForeground(Color.BLUE);
} else if ((arg0.getValueAt(arg4, 2)).equals("")
&& (!(arg0.getValueAt(arg4, 0).equals("")))) {
setForeground(Color.ORANGE);
}
setText(arg1.toString());
setBackground(Color.WHITE);
return this;
}}2. class ColorTableRowRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable arg0, Object arg1,
boolean arg2, boolean arg3, int arg4, int arg5) {
if ((arg0.getValueAt(arg4, 0)).equals(arg0.getValueAt(arg4, 2))) {
if ((arg0.getValueAt(arg4, 1)).equals(arg0.getValueAt(arg4, 3))) {
setForeground(Color.RED);
} else {
setForeground(Color.red);
}
} else if ((arg0.getValueAt(arg4, 0)).equals("")
&& (!(arg0.getValueAt(arg4, 2).equals("")))) {
setForeground(Color.BLUE);
} else if ((arg0.getValueAt(arg4, 2)).equals("")
&& (!(arg0.getValueAt(arg4, 0).equals("")))) {
setForeground(Color.ORANGE);
}
setText(arg1.toString());
setBackground(Color.WHITE);
return this;
}}
class ColorTableRowRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
if ((arg0.getValueAt(arg4, 0)).equals(arg0.getValueAt(arg4, 2))) {
if ((arg0.getValueAt(arg4, 1)).equals(arg0.getValueAt(arg4, 3))) {
setForeground(Color.RED);
} else {
setForeground(Color.red);
}
} else if ((arg0.getValueAt(arg4, 0)).equals("") && (!(arg0.getValueAt(arg4, 2).equals("")))) {
setForeground(Color.BLUE);
} else if ((arg0.getValueAt(arg4, 2)).equals("") && (!(arg0.getValueAt(arg4, 0).equals("")))) {
setForeground(Color.ORANGE);
} else {
setForeground(Color.BLACK);
}
setText(arg1.toString());
setBackground(Color.WHITE);
return super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5);
} }