简体   繁体   English

如何根据先前的单元格值更改JTable的单元格背景?

[英]How to change a cell backgound of JTable based on previous cell value?

I'm trying to change the background of a JTable cells, based on the value of the cell of the column -1. 我正在尝试根据列-1的单元格的值更改JTable单元格的背景。

So if a value of cell at[row, Column] is diffrent from given values eg "A", "B", then change the background of the given cell and the cell at [row, column+1] 因此,如果单元格at [row,Column]的值与给定值(例如“ A”,“ B”)不同,则更改给定单元格和[row,column + 1]处的单元格的背景

Here is what I get now: 这是我现在得到的:

在此处输入图片说明

What I want is the following : 我想要的是以下内容:

在此处输入图片说明

I used a custom cell rendered, but I can't achieve this requirements. 我使用了渲染的自定义单元格,但无法满足此要求。

This is my code: 这是我的代码:

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class TestCellRendered {

    private JFrame frame;
    private JTable table;

    private DefaultTableModel model;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestCellRendered window = new TestCellRendered();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     * @throws IOException 
     */
    public TestCellRendered() throws IOException {
        initialize();

    }



    /**
     * Initialize the contents of the frame.
     * @throws IOException 
     */
    private void initialize() throws IOException {
        frame = new JFrame();
        frame.setResizable(false);
        frame.setBounds(100, 100, 975, 756);
        frame.getContentPane().setLayout(null);

        model = new DefaultTableModel();
        model.addColumn("À remplacer");
        model.addColumn("Lignes");
        table = new JTable(model);
        MyRenderer myRenderer = new MyRenderer();   // See below
        table.setDefaultRenderer(Object.class   , myRenderer);
        table.setBackground(Color.LIGHT_GRAY);
        Font font = new Font("",1,14);
        table.setForeground(Color.black);
        table.setFont(font);
        //Add rows to my table
        model.addRow(new Object[]{"a",""});
        model.addRow(new Object[]{"b", ""});
        model.addRow(new Object[]{"c", ""});


        JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(66, 66, 519, 421);
        frame.getContentPane().add(scrollPane);


    }
    class MyRenderer extends DefaultTableCellRenderer  
    { 
        public Component getTableCellRendererComponent(JTable table, Object value, boolean   isSelected, boolean hasFocus, int row, int column) 
    { 
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 

        if (! table.isRowSelected(row))
        {
            if( !(value.equals("a") |value.equals("b") ))
                {
                c.setBackground(new java.awt.Color(231, 76, 60));
                c.setForeground(new  java.awt.Color(255,255,255));
                }
            else{
                c.setBackground(table.getBackground());
                c.setForeground(table.getForeground());
            }
        }


        return c; 
    } 

    } 
}

based on the value of the cell of the column -1. 基于列-1的单元格的值。

if( !(value.equals("a") |value.equals("b") ))

Well you are always doing the test on the current column. 好吧,您总是在当前列上进行测试。 You need to do the test on the first column: 您需要在第一列上进行测试:

String text = table.getValueAt(row, 0).toString();

if( !(text.equals("a") |text.equals("b") ))

Note it is usually easier to write code in a if statement with a positive test: 请注意,通常在带有肯定测试的if语句中编写代码更容易:

if (text.equals("a" || text.equals("b"))
{
    c.setBackground(table.getBackground());
    c.setForeground(table.getForeground());
}
else
{
    c.setBackground(new java.awt.Color(231, 76, 60));
    c.setForeground(new  java.awt.Color(255,255,255));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM