简体   繁体   English

Java表CustomTableCellRenderer根据单元格内容值更改行颜色

[英]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. 我正在开发Java应用程序。我想在具有3列名称课程和年份的表中显示数据。我根据互联网上的教程使用tablerenderer。此代码实际上是根据行号用青色和灰色为行着色..但是我想仅在年列(年值)等于某个值的情况下才将青色设置为行,比如说3,这意味着如果第三行年列值等于5,则第三行颜色应为青色,否则应为灰色。 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 您可以像在CustomTableCellRenderer类中那样简单地进行操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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