Table 渲染器,其中有 2 个 if 语句

Table renderer where there are 2 if statements

我有一个 table 渲染器,它根据第 11 列的内容在我的 table 中生成一行红色。这工作正常,代码如下:

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" 该特定行为黄色。我的尝试在下面,但是现在 table 中根本没有颜色出现。除此之外,如果第 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;

}

根据您有点奇怪的代码片段,我创建了以下内容。不能说我明白为什么你会在第 11 列和第 12 列的值上设置颜色...

注:

  • 使用 JTable.getValueAt 而不是使用 TableModel.getValueAt
  • 使用 JTable.convertColumnIndexToView 因为我想第 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);
            }
        });
    }
}

结果:

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;
}
}

给你,我希望这能解决问题