簡體   English   中英

如何根據先前的單元格值更改JTable的單元格背景?

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

我正在嘗試根據列-1的單元格的值更改JTable單元格的背景。

因此,如果單元格at [row,Column]的值與給定值(例如“ A”,“ B”)不同,則更改給定單元格和[row,column + 1]處的單元格的背景

這是我現在得到的:

在此處輸入圖片說明

我想要的是以下內容:

在此處輸入圖片說明

我使用了渲染的自定義單元格,但無法滿足此要求。

這是我的代碼:

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

    } 
}

基於列-1的單元格的值。

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

好吧,您總是在當前列上進行測試。 您需要在第一列上進行測試:

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

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

請注意,通常在帶有肯定測試的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