java 如何在java中更改JTable中单个单元格的背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10755685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:24:55  来源:igfitidea点击:

how to change background color of individual cell in JTable in java?

javaswingjtabletablecellrenderer

提问by itro

I search in the table, when i find a match i want to change a bg color of that cell. I did as below but still can't fix it? Can any body help to fix this problem?

我在表格中搜索,当我找到匹配项时,我想更改该单元格的背景颜色。我做了如下但仍然无法修复它?任何机构都可以帮助解决这个问题吗?

public class SearchTable extends JTable {
JTable table;
JTextField textField;

public SearchTable(JTable table, JTextField textField) {
    this.table = table;
    this.textField = textField;

    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            search();
        }
    });

    textField.getDocument().addDocumentListener(new DocumentListener() {
        public void insertUpdate(DocumentEvent e) {
            search();
        }
        public void removeUpdate(DocumentEvent e) {
            search();
        }
        public void changedUpdate(DocumentEvent e) {
            search();
        }
    });
}

private void search() {
    String target = textField.getText();
    for (int row = 0; row < table.getRowCount(); row++)
        for (int col = 0; col < table.getColumnCount(); col++) {
            String next = (String) table.getValueAt(row, col);
            if (next.equals(target)) {
                changeBackgroundColor(row, col);
                return;
            }
        }
    table.repaint();
}

private void changeBackgroundColor(int row, int col) {
    table.setColumnSelectionAllowed(true);
    table.setRowSelectionAllowed(true);
    boolean toggle = false;
    boolean extend = false;
    table.changeSelection(row, col, toggle, extend);
    //first atempt sets bg color for all cells, it is not OK
    //table.setSelectionBackground(Color.green);

    //second atempt getting no result
    table.getCellEditor(row,col).getTableCellEditorComponent(table,table.getValueAt(row,col),true,row,col).setForeground(Color.red);

    //3th atempt getting no result
    //Component c = table.getCellRenderer(row, col).getTableCellRendererComponent(table, table.getValueAt(row, col), true, true, row, col);
    //c.setForeground(Color.red);

    //4th atempt getting no result
    //DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) table.getCellRenderer(row, col).getTableCellRendererComponent(table, table.getValueAt(row, col), true, true, row, col).;
      //renderer.setBorder(new LineBorder(Color.red));
}

   }

回答by mKorbel

You can use XxxCellRenderer, better and easiest is to use prepareRenderer()

您可以使用XxxCellRenderer,更好和最简单的是使用prepareRenderer()

for correct code you have to override or test inside if-else follows patameters

要获得正确的代码,您必须在 if-else 后面的参数中覆盖或测试

  • isSelected

  • hasFocus

  • column

  • row

  • 已选择

  • 有焦点

  • 柱子

Please to check answersand one questionabout similair issue, there are two simple ways, sorry I‘m in FRI trafic, brrrrr

请以检查答案和一个问题有关similair问题上,有两种简单的方法,对不起,我在周五TRAFIC,brrrrr

回答by Mohsin

You need to set the cell renderer to the column first -

您需要先将单元格渲染器设置为列 -

col.setCellRenderer( new TableCellRenderer() {
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) 
                {
                    Component cell = centerRenderer.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
    cell.setForeground(Color.green);
}
});