我试图根据单元格值更改行颜色

时间:2017-03-19 22:56:09

标签: java swing jtable

我试图根据单元格值更改行颜色,这是我的代码

这是数据库的连接,以便在jtable中显示它

import java.sql.*;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import java.awt.Color;
import java.awt.Component;
  public class Rechercher extends javax.swing.JFrame {
 Statement stmt;
 connexion maconnexion = new connexion();
  ResultSet Rs;
    PreparedStatement  ps =null;
   Connection conn = null;
   DefaultTableModel model=new DefaultTableModel();
  public Rechercher() {
    initComponents();                  
   model.addColumn("Code Piece");
   model.addColumn("Reference");
   model.addColumn("Nom");
   model.addColumn("Marque");
   model.addColumn("Dimention");
   model.addColumn("Quantite");
   model.addColumn("Categorie");
   model.addColumn("Type");
   model.addColumn("Discription");
   model.addColumn("Famille");
    model.addColumn("Sous Famille");
     try {
    stmt=maconnexion.Obtenirconnexion().createStatement();
     ResultSet Rs=stmt.executeQuery("Select 
     piece.*,famille.Nom_F,sousfamille.Nom_SF from piece left join famille 
     ON famille.Code_Piece=piece.Code_Piece left join sousfamille on 
     sousfamille.Code_Piece = piece.Code_Piece order by Code_Piece");
     while(Rs.next()){
      model.addRow(new Object[]
      {Rs.getString("Code_Piece"),Rs.getString("Reference"),
      Rs.getString("Nom_P"),
      Rs.getString("Mark_P"),Rs.getString("Dimention") 
      ,Rs.getString("Quantite"),Rs.getString("Categorie"),
       Rs.getString("Type"),Rs.getString("Discription"),
       Rs.getString("Nom_F"),Rs.getString("Nom_SF")}); }
           }catch(Exception e){System.err.println(e);}
       jRech.setModel(model);     
       jRech.setAutoCreateRowSorter(true); }  

这就是我用来改变单元格的颜色取决于一个值 问题是"组件c"有错误说"找不到符号"

           public Component prepareRenderer (TableCellRenderer renderer, int 
       rowIndex, int columnIndex){
      Component c = super.prepareRenderer(renderer, rowIndex, columnIndex);
      Object value = jRech.getModel().getValueAt(rowIndex,columnIndex);
       if(columnIndex == 6){
        if(value.equals("0"))
        {
            c.setBackground(Color.GREEN);
            c.setForeground(Color.BLACK);     
        }}return c;
        } 

1 个答案:

答案 0 :(得分:0)

你可以这样做:

 public static void setOneRowBackgroundColor(JTable table, int rowIndex,  
            Color color) {  
        try {  
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {  

                public Component getTableCellRendererComponent(JTable table,  
                        Object value, boolean isSelected, boolean hasFocus,  
                        int row, int column) {  
                    if (row == rowIndex) {  
                        setBackground(color);  
                        setForeground(Color.WHITE);  
                    }else if(row > rowIndex){  
                        setBackground(Color.BLACK);  
                        setForeground(Color.WHITE);  
                    }else{  
                        setBackground(Color.BLACK);  
                        setForeground(Color.WHITE);  
                    }  

                    return super.getTableCellRendererComponent(table, value,  
                            isSelected, hasFocus, row, column);  
                }  
            };  
            int columnCount = table.getColumnCount();  
            for (int i = 0; i < columnCount; i++) {  
                table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);  
            }  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }