我有一个表格渲染器,根据第11列的内容,我的表格中的一行是红色的。这样工作正常,代码如下:
tableR = new JTable(modelR)
{
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
Font myFont = new Font("Arial",Font.PLAIN,10);
Font myFont1 = new Font("Arial", Font.BOLD,10);
int rowModelId = convertRowIndexToModel( row );
if (!isRowSelected(row)) {
if (tableR.getColumnCount() >= 0) {
String type = (String) getModel().getValueAt(rowModelId, 11);
c.setBackground("0.0".equals(type) ? Color.RED : Color.WHITE);
c.setForeground("0.0".equals(type) ? Color.WHITE : Color.BLACK);
c.setFont("0.0".equals(type) ? myFont1: myFont);
}
}
return c;
}
我现在想要在第12列中另外实现相同的功能,这样如果满足条件,在这种情况下“u”表示特定行为黄色。我的尝试在下面,但现在表中根本没有颜色出现。除此之外,如果第11列和第12列是彩色的,那么在这种情况下会发生什么?
这是我的尝试去:
tableR = new JTable(modelR)
{
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
Font myFont = new Font("Arial",Font.PLAIN,10);
Font myFont1 = new Font("Arial", Font.BOLD,10);
int rowModelId = convertRowIndexToModel( row );
int rowModelId1 = convertRowIndexToModel( row );
if (!isRowSelected(row)) {
if (tableR.getColumnCount() >= 0) {
String type = (String) getModel().getValueAt(rowModelId, 11);
c.setBackground("0.0".equals(type) ? Color.RED : Color.WHITE);
c.setForeground("0.0".equals(type) ? Color.WHITE : Color.BLACK);
c.setFont("0.0".equals(type) ? myFont1: myFont);
}
if (tableR.getColumnCount() >= 0) {
String type = (String) getModel().getValueAt(rowModelId1, 12);
c.setBackground("u".equals(type) ? Color.YELLOW : Color.WHITE);
c.setForeground("u".equals(type) ? Color.WHITE : Color.BLACK);
c.setFont("u".equals(type) ? myFont1: myFont);
}
}
return c;
}
答案 0 :(得分:1)
根据您的有些怪异代码段,我创建了以下内容。不能说我理解你为什么要在第11栏和第11栏上添加颜色。 12个值......
注意:
JTable.getValueAt
代替TableModel.getValueAt
JTable.convertColumnIndexToView
因为我认为第11栏和第11栏12你的意思是模型中的那些,而不是视图(视图和模型索引在视图中的列周围移动时会发生变化)import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
public class example {
static Font myFont = new Font("Arial",Font.PLAIN,10);
static Font myFont1 = new Font("Arial", Font.BOLD,10);
private static Component createTable() {
Object rowData[][] = new Object[][]{
{"0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0"},
{"b","6.70","q","l","b","6.70","q","l","b","6.70","q","l","p"},
{"0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0"},
{"u","u","u","u","u","u","u","u","u","u","u","u","u"},
{"b","6.70","q","l","b","6.70","q","l","b","6.70","q","l","p"},
{"u","u","u","u","u","u","u","u","u","u","u","u","u"},
};
Object colData[] = {"Col1","Col2","Col3","Col4","Col5","Col6","Col7","Col8","Col9","Col10","Col11","Col12","Col13"};
return new JTable( rowData, colData ) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (isRowSelected(row) || getColumnCount()==0)
return c;
String type = (String) getValueAt(row, convertColumnIndexToView( 11 ));
if("0.0".equals(type))
{
c.setBackground(Color.RED);
c.setForeground(Color.WHITE);
c.setFont(myFont1);
return c;
}
type = (String) getValueAt( row, convertColumnIndexToView( 12 ) );
if("u".equals(type))
{
c.setBackground(Color.YELLOW);
c.setForeground(Color.WHITE);
c.setFont(myFont1);
return c;
}
c.setBackground(Color.WHITE);
c.setForeground(Color.BLACK);
c.setFont(myFont);
return c;
}
};
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.add(new JScrollPane(createTable()), BorderLayout.CENTER);
f.setSize(500, 500);
f.setVisible(true);
}
});
}
}
结果:
答案 1 :(得分:1)
tableR = new JTable(modelR) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
Font myFont = new Font("Arial",Font.PLAIN,10);
Font myFont1 = new Font("Arial", Font.BOLD,10);
int rowModelId = convertRowIndexToModel( row );
int rowModelId1 = convertRowIndexToModel( row );
if (!isRowSelected(row)) {
if (tableR.getColumnCount() >= 0) {
String type = (String) getModel().getValueAt(rowModelId1, 12);
if("u".equals(type)) {
c.setBackground(Color.YELLOW);
c.setForeground(Color.WHITE);
c.setFont(myFont1);
return c;
}
type = (String) getModel().getValueAt(rowModelId, 11);
if("0.0".equals(type)) {
c.setBackground(Color.RED);
c.setForeground(Color.WHITE);
c.setFont(myFont1);
return c;
}
}
c.setBackground(Color.WHITE);
c.setForeground(Color.BLACK);
c.setFont(myFont);
}
return c;
}
}
你走了,我希望这能解决它