Changing the color of a JTable cell DYNAMICALLY

天大地大妈咪最大 提交于 2021-01-28 08:41:38

问题


I would like to know how do i change the background color of a cell in a Table without any response from users. For example, if there is a change in one of my variables due to some unrelated method calls, which I would like for to be reflected in my table. Example, a method changes a value of my array that I used to initialize my table, however the user had no hand in it, like say a elapsed time change in value. So how do I reflect it on my table, or is there any way I could manually call the TableCellRenderer

EDIT: updated question with code snippets here


回答1:


As advised use a custom cell renderer:

import java.awt.Color;
import java.awt.Component;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;

public class CellRenderDemo {

    private static final int MAX =120;
    private static Object[] columnName = {"Item", "Value"};
    private static Object[][] data = {{"Value A", 12}, {"Value B", 34},  {"Value C", 66}};
    private final JTable table;

    CellRenderDemo() {
        table = new JTable(data, columnName);
        table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer());//use custom cell rendered for col 1
    }

    void randomizeData(){
        Random rnd = new Random();
        TableModel model = table.getModel();
        new Timer(1000, e->{
            model.setValueAt(rnd.nextInt(MAX+1), rnd.nextInt(model.getRowCount()), 1);
        }).start();
    }

    JTable getTable() {
        return table;
    }

    static class CustomRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                int row, int column)   {
            Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            int cellValue = (int)value;
            if(cellValue <= MAX / 3){ //change color based on value 
                cellComponent.setBackground(Color.PINK);
            } else if ( cellValue > 2*MAX / 3){
                cellComponent.setBackground(Color.CYAN);
            } else {
                cellComponent.setBackground(Color.YELLOW);
            }
            return cellComponent;
        }
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            JFrame frame = new JFrame();
            CellRenderDemo cd = new CellRenderDemo();
            frame.add(new JScrollPane(cd.getTable()));
            frame.pack();
            cd.randomizeData();
            frame.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}



来源:https://stackoverflow.com/questions/61703895/changing-the-color-of-a-jtable-cell-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!