【发布时间】: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