简体   繁体   中英

java table CustomTableCellRenderer change row color according to cell content value

i am developing java application .i want to show data in a table which has 3 column name course and year .i use tablerenderer according to a tutorial found on internet.this code actually doing is colorize rows with cyan and gray color according to row number ..but i want to set cyan color to row only if year column (value of year) equals to certain value let's say 3.which mean if 3rd row year column value equal 5 then 3rd row color should cyan else it should gray. this is my code i found on internet ..so how can i modify it for my goal ??

import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;


public class CustomCellRenderer{
  JTable table;
  TableColumn tcol;
  public static void main(String[] args) {
  new CustomCellRenderer();
  }

  public CustomCellRenderer(){
  JFrame frame = new JFrame("Creating a Custom Cell Reanderer!");
  JPanel panel = new JPanel();
  String data[][] = {{"Vinod","Computer","3"},
   {"Rahul","History","2"},
   {"Manoj","Biology","5"},
   {"Sanjay","PSD","6"}};
  String col [] = {"Name","Course","Year"};
  DefaultTableModel model = new DefaultTableModel(data,col);
  table = new JTable(model);
  tcol = table.getColumnModel().getColumn(0);
  tcol.setCellRenderer((TableCellRenderer) new CustomTableCellRenderer());
  tcol = table.getColumnModel().getColumn(1);
  tcol.setCellRenderer((TableCellRenderer) new CustomTableCellRenderer());
  tcol = table.getColumnModel().getColumn(2);
  tcol.setCellRenderer((TableCellRenderer) new CustomTableCellRenderer());
  JTableHeader header = table.getTableHeader();
  header.setBackground(Color.yellow);
  JScrollPane pane = new JScrollPane(table);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(500,150);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }

  public class CustomTableCellRenderer extends DefaultTableCellRenderer{
  public Component getTableCellRendererComponent (JTable table, 
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
  Component cell = super.getTableCellRendererComponent(
   table, obj, isSelected, hasFocus, row, column);

  if (isSelected) {
  cell.setBackground(Color.green);
  } 
  else {
  if (row % 2 == 0 ) {
  cell.setBackground(Color.cyan);
  }
  else {
  cell.setBackground(Color.lightGray);
  }
  }
  return cell;
  }
  }
}

but i want to set cyan color to row only if year column (value of year) equals to certain value

Don't use individual renderers. Instead you can use the approach found in Table Row Rendering .

You can do it simply as in CustomTableCellRenderer class

TableModel model = table.getModel();
String colYear = model.getColumnName(2);
int colYearValue = Integer.valueOf((String) model.getValueAt(row, 2));

if (colYearValue == 3) {
    cell.setBackground(Color.cyan);
} else {
    cell.setBackground(Color.lightGray);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM