Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中更改JTable中单个单元格的背景色?_Java_Swing_Jtable_Tablecellrenderer - Fatal编程技术网

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

如何在java中更改JTable中单个单元格的背景色?,java,swing,jtable,tablecellrenderer,Java,Swing,Jtable,Tablecellrenderer,我在表格中搜索,当我找到匹配项时,我想更改该单元格的背景颜色。 我做了如下操作,但仍然无法修复它?有人能帮助解决这个问题吗 public class SearchTable extends JTable { JTable table; JTextField textField; public SearchTable(JTable table, JTextField textField) { this.table = table; this.textField = textFie

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

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

   }
公共类SearchTable扩展了JTable{
JTable表;
JTextField-textField;
公共搜索表(JTable表,JTextField textField){
this.table=表格;
this.textField=textField;
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
搜索();
}
});
textField.getDocument().addDocumentListener(新DocumentListener()){
公共作废插入更新(文档事件e){
搜索();
}
公共作废移除更新(文档事件e){
搜索();
}
公共作废更改日期(记录事件e){
搜索();
}
});
}
私有无效搜索(){
String target=textField.getText();
对于(int row=0;row
您可以使用
XxxCellRenderer
,更好、更简单的方法是使用
prepareRenderer()

要获得正确的代码,必须重写或在内部测试,否则将遵循参数

  • 当选

  • 哈斯福克斯

  • 纵队

  • 划船


请检查一个关于similair的问题,有两个简单的方法,抱歉我在FRI trafic,brrrrrr

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

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

我认为,正如建议的那样,覆盖
prepareRenderer()
对于单个单元格来说可能更容易。集中精力驾驶。;)@Andrew Thompson eeerrrghhtt终于我在家了:-),在手机上写东西太可怕了