且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java JTable更改单元格颜色

更新时间:2022-10-23 09:57:53

假设您要用不同颜色渲染的单元格代表一个状态(我将以被拒绝和被批准为例)。然后,我将在我的表模型中实现一个名为getStatus(int row)的方法,返回任何给定行的状态。



创建一个单元格渲染器负责渲染单元格所属的列。

  public class StatusColumnCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int col){

//单元格默认呈现为JLabel。
JLabel l =(JLabel)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);

//获取当前行的状态。
CustomTableModel tableModel =(CustomTableModel)table.getModel();
if(tableModel.getStatus(row)== CustomTableModel.APPROVED){
l.setBackground(Color.GREEN);
} else {
l.setBackground(Color.RED);
}

//返回呈现单元格的JLabel。
return l;

}

然后,当渲染器到位时, renderer to the table with the following piece of code:

  Table.getColumnModel()。getColumn(columnIndex).setCellRenderer new StatusColumnCellRenderer()); 

关于使单元格可编辑,只需实现 isCellEditable (int rowIndex,int columnIndex)表模型。您还需要实现方法
setValueAt (对象值,int rowIndex,int columnIndex)如果你想保留用户提供的值(我假设你做! / p>

I would like to make an editable table and then check the data to make sure its valid. Im not sure how to change the color of just one cell. I would like to get a cell, for example (0,0) and color the foreground to red. I have read the other posts on SO as well as Oracle about the custom ColorRenderer, but i just don't get how i would use this.

Thanks.

Say that the cell you would like to render with a different color represents a status (I'll take Rejected and Approved as examples). I'd then implement a method in my table model called getStatus(int row) which returns the status for any given row.

Then, when that is in place, I'd go about creating a cell renderer responsible for rendering the column which the cell belongs to. The cell renderer would be something in the lines of the below code.

public class StatusColumnCellRenderer extends DefaultTableCellRenderer {
  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {

    //Cells are by default rendered as a JLabel.
    JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);

    //Get the status for the current row.
    CustomTableModel tableModel = (CustomTableModel) table.getModel();
    if (tableModel.getStatus(row) == CustomTableModel.APPROVED) {
      l.setBackground(Color.GREEN);
    } else {
      l.setBackground(Color.RED);
    }

  //Return the JLabel which renders the cell.
  return l;

}

Then, when the renderer is in place, simply "apply" the renderer to the table with the following piece of code:

Table.getColumnModel().getColumn(columnIndex).setCellRenderer(new StatusColumnCellRenderer());

With regard to making a cell editable, simply implement the isCellEditable(int rowIndex, int columnIndex) method in your table model. You also need to implement the method setValueAt(Object value, int rowIndex, int columnIndex) if you would like to keep the value which the user provides (which i assume you do!).