【问题标题】:Change rows' color in Java JTable在 Java JTable 中更改行的颜色
【发布时间】:2019-03-07 12:08:01
【问题描述】:

我正在尝试更改 Java JTable 中行的颜色,但遇到了一些问题。这是场景:我有一个包含一些数据要处理的表格,我希望在处理结束时(通过按钮开始),通过根据结果将线条着色为绿色、黄色或红色来更新表格的操作。每个处理过的对象都有一个在处理后设置的变量“结果”。该表是由 Netbeans 的图形编辑器创建的(因此无法修改自动生成的代码)。我使用了这个 TableModel:

public class QuotationsTableModel extends AbstractTableModel {

    private List<Quotation> quotationsList;

    public QuotationsTableModel(List<Quotation> quotationsList) {
        this.quotationsList= quotationsList;
    }

    @Override
    public int getRowCount() {
        if (quotationsList== null) {
            return 0;
        }
        return this.quotationsList.size();
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (quotationsList== null) {
            return null;
        }
        Quotation quotation = quotationsList.get(rowIndex);
        if (columnIndex == 0) {
            return quotation.getQuotationNumber();
        }
        if (columnIndex == 1) {
            return quotation.getBillingType();
        }
        if (columnIndex == 2) {
            return quotation.getAdvance();
        }
        if (columnIndex == 3) {
            return quotation.getOutcome();
        }
        return null;
    }

    @Override
    public String getColumnName(int column) {
        if (column == 0) {
            return "Number";
        } else if (column == 1) {
            return "Billing type";
        } else if (column == 2) {
            return "Advance";
        } else if (column == 3) {
            return "Outcome";
        }
        return null;
    }

    public void updateTable() {
        this.fireTableDataChanged();
    }

我试图通过创建类来达到目标​​:

public class CustomTableRenderer extends DefaultTableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        Component original = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        Color background = table.getBackground();
        //Color grid = Color.YELLOW;

        Color fg = null;
        Color bg = null;


        if (isSelected) {
            super.setForeground(fg == null ? table.getSelectionForeground()
                                           : fg);
            super.setBackground(bg == null ? table.getSelectionBackground()
                                           : bg);
        } else {
            if (column == 3) {
                String outcome = String.valueOf(value);
                if (outcome .equalsIgnoreCase("COMPLETED")){
                  background = Color.GREEN;
                } else if (outcome .equalsIgnoreCase("PARTIAL")) {
                    background = Color.YELLOW;
                } else if (outcome .equalsIgnoreCase("ERROR")) {
                    background = Color.RED;
                }
            }
        }
    original.setBackground(background);

    return original;
    }

然后调用:

QuotationsTableModel quotationsTableModel= new QuotationsTableModel(quotationsList);
this.quotationsTable.setModel(quotationsTableModel);
this.quotationsTable.setDefaultRenderer(Object.class, new CustomTableRenderer());

但只有在选择线时结果才会有颜色,此外,一旦选择了线,除了结果之外的所有值都会消失。你能帮帮我吗?

我找到了一个可行的解决方案,也许它对那些即将到来的人有用:

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class CustomTableCellRenderer extends DefaultTableCellRenderer {


    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {


        Component original = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        Color background = table.getBackground();

        if (isSelected) {            
            original.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.selectionBackground"));
            original.setForeground(javax.swing.UIManager.getDefaults().getColor("TextField.selectionForeground"));
            table.setRowSelectionInterval(row, row);            
        } else {
            original.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.highlight"));
            original.setForeground(Color.BLACK);        
            if (column == 3) {
                String outcome = String.valueOf(value);
                if (outcome.equalsIgnoreCase("COMPLETED")){
                  background = Color.GREEN;
                } else if (outcome.equalsIgnoreCase("PARTIAL")) {
                    background = Color.YELLOW;
                } else if (outcome.equalsIgnoreCase("ERROR")) {
                    background = Color.RED;
                }
                original.setBackground(background);
            }
        }

    return original;
    }
}

谢谢。

【问题讨论】:

  • 您能解释一下可行的解决方案吗?我只能看到你注释掉了CustomTableCellRenderer的正文。着色如何与它一起使用?
  • 抱歉,评论有误

标签: java colors jtable row customization


【解决方案1】:

我使用您的QuotationsTableModelCustomTableRenderer 类创建了以下示例程序。我看到的唯一问题是CustomTableRenderer.getTableCellRendererComponent() 方法。

您仅在未选择该行时才更改“结果”列颜色。因此,选择行时着色不起作用。为了解决这个问题,我删除了if (isSelected) { 部分代码。运行下面的例子看看。

(以后发布问题时,请发布一个最小的可执行程序,例如下面的示例程序。这样其他人可以更轻松地运行该场景并查看问题。)

import javax.swing.*;
import javax.swing.table.*;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

public class ColorTable {

  public static void main(String[] args) {
    List<Quotation> quotationsList = new ArrayList<>();
    quotationsList.add(new Quotation(111, "AA", 2500, "COMPLETED"));
    quotationsList.add(new Quotation(222, "BB", 4000, "PARTIAL"));
    quotationsList.add(new Quotation(333, "CC", 5000, "COMPLETED"));
    quotationsList.add(new Quotation(444, "DD", 1500, "SOME_OTHER_OUTCOME"));
    quotationsList.add(new Quotation(555, "EE", 3500, "ERROR"));
    QuotationsTableModel quotationsTableModel= new QuotationsTableModel(quotationsList);

    JTable quotationsTable = new JTable();
    quotationsTable.setModel(quotationsTableModel);
    quotationsTable.setDefaultRenderer(Object.class, new CustomTableRenderer());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JScrollPane(quotationsTable));
    frame.pack();
    frame.setVisible(true);
  }
}

class QuotationsTableModel extends AbstractTableModel {

  private List<Quotation> quotationsList;

  public QuotationsTableModel(List<Quotation> quotationsList) {
    this.quotationsList= quotationsList;
  }

  @Override
  public int getRowCount() {
    if (quotationsList== null) {
      return 0;
    }
    return this.quotationsList.size();
  }

  @Override
  public int getColumnCount() {
    return 4;
  }

  @Override
  public Object getValueAt(int rowIndex, int columnIndex) {
    if (quotationsList== null) {
      return null;
    }
    Quotation quotation = quotationsList.get(rowIndex);
    if (columnIndex == 0) {
      return quotation.getQuotationNumber();
    }
    if (columnIndex == 1) {
      return quotation.getBillingType();
    }
    if (columnIndex == 2) {
      return quotation.getAdvance();
    }
    if (columnIndex == 3) {
      return quotation.getOutcome();
    }
    return null;
  }

  @Override
  public String getColumnName(int column) {
    if (column == 0) {
      return "Number";
    } else if (column == 1) {
      return "Billing type";
    } else if (column == 2) {
      return "Advance";
    } else if (column == 3) {
      return "Outcome";
    }
    return null;
  }

  public void updateTable() {
    this.fireTableDataChanged();
  }
}

class CustomTableRenderer extends DefaultTableCellRenderer {

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

    Component original = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    Color background = table.getBackground();
    //Color grid = Color.YELLOW;

    if (column == 3) {
      String outcome = String.valueOf(value);
      if (outcome .equalsIgnoreCase("COMPLETED")){
        background = Color.GREEN;
      } else if (outcome .equalsIgnoreCase("PARTIAL")) {
        background = Color.YELLOW;
      } else if (outcome .equalsIgnoreCase("ERROR")) {
        background = Color.RED;
      }
    }
    original.setBackground(background);

    return original;
  }
}

class Quotation {
  private int quotationNumber;
  private String billingType;
  private int advance;
  private String outcome;

  Quotation(int quotationNumber, String billingType, int advance, String outcome) {
    this.quotationNumber = quotationNumber;
    this.billingType = billingType;
    this.advance = advance;
    this.outcome = outcome;
  }

  int getQuotationNumber() {
    return quotationNumber;
  }

  String getBillingType() {
    return billingType;
  }

  int getAdvance() {
    return advance;
  }

  String getOutcome() {
    return outcome;
  }
}

输出:

【讨论】:

  • 非常感谢您的回答。由于某种原因,这个解决方案对我不起作用,我已经尝试过了。我编辑了第一篇添加我的解决方案的帖子,也许它对那些即将到来的人有用。谢谢。
猜你喜欢
  • 2015-04-24
  • 2011-08-06
  • 2013-05-28
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 2019-09-09
相关资源
最近更新 更多