我想根据第1列中的值为我的表格单元着色,如果值不等于第1列中的值,则颜色应为青色,但我的代码不起作用:
table = new JTable(){
public TableCellRenderer getCellRenderer(int row, int column) {
TableCellRenderer tcr=null;
Color c;
if(column>=1&&getValueAt(row, column)!=null &&getValueAt(row, 1)!=null &&!getValueAt(row, column).equals(getValueAt(row, 1)))
c=Color.CYAN;
else
c=Color.white;
if(getValueAt(row, column) instanceof Boolean) {
tcr= super.getDefaultRenderer(Boolean.class);
} else {
tcr= super.getCellRenderer(row, column);
}
tcr.getTableCellRendererComponent(this, getValueAt(row, column), isCellSelected(row, column) , hasFocus(), row, column).setBackground(c);
return tcr;
}
public TableCellEditor getCellEditor(int row, int column) {
if(getValueAt(row, column) instanceof Boolean) {
return super.getDefaultEditor(Boolean.class);
} else {
return super.getCellEditor(row, column);
}
}
};
table.setModel(new DefaultTableModel(
new Object[][] {
{"Row1", "1","2","3"},
{"Row2", "2","2","3"},
{"Row3", "2","2","2"},
{"Row3", "2","1","2"}
},
new String[] {"Header1", "Header2","Header3","Header4"}
));
答案 0 :(得分:3)
必须在TableCellRenderer中决定是否必须突出显示单元格。它不能在JTable中制作。所以(可能)没有理由重写JTable中的方法。相反,您可以将列的CellRenderer设置为您自己的TableCellRenderer的implmentntation。
在此示例中,第2列和第3列接收“ColoringCellRenderer”。如果此单元格中的值不为null且与第1列中的值相同,则此ColoringCellRenderer将突出显示某个单元格。
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class ColoredTableCells
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(createTable()));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
static JTable createTable()
{
class ColoringTableCellRenderer extends DefaultTableCellRenderer
{
ColoringTableCellRenderer()
{
setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
if (highlightCell(table, row, column))
{
setBackground(Color.CYAN);
}
else
{
setBackground(Color.WHITE);
}
return this;
}
private boolean highlightCell(JTable table, int row, int column)
{
Object thisValue = table.getValueAt(row, column);
Object referenceValue = table.getValueAt(row, 1);
if (thisValue != null && referenceValue != null)
{
return thisValue.equals(referenceValue);
}
return false;
}
}
JTable table = new JTable();
table.setModel(new DefaultTableModel(new Object[][] {
{ "Row1", "1", "2", "3" },
{ "Row2", "2", "2", "3" },
{ "Row3", "2", "2", "2" },
{ "Row3", "2", "1", "2" } },
new String[] { "Header1", "Header2", "Header3", "Header4" }));
for (int i=2; i<table.getColumnCount(); i++)
{
TableColumn column = table.getColumnModel().getColumn(i);
column.setCellRenderer(new ColoringTableCellRenderer());
}
return table;
}
}
编辑:答案已被接受,但在回应评论时,不同的版本:
这个使用两个类,“DelegatingTableCellRenderer”和“DelegatingTableCellEditor”。这些类每个都接收相应的委托,并且仅设置委托传递的单元组件的背景颜色。
这样,着色表格单元格(用于渲染器或编辑器)的功能与实际渲染器分离。因此,可以为Boolean
值(显示JCheckBox)的默认CellRenderer的背景着色。
import java.awt.Color;
import java.awt.Component;
import java.util.EventObject;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class ColoredTableCells2
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(createTable()));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
static JTable createTable()
{
JTable table = new JTable()
{
@Override
public Class<?> getColumnClass(int column)
{
if (column == 0)
{
return Object.class;
}
return Boolean.class;
}
};
table.setDefaultEditor(Boolean.class,
new DelegatingTableCellEditor(table.getDefaultEditor(Boolean.class)));
table.setDefaultRenderer(Boolean.class,
new DelegatingTableCellRenderer(table.getDefaultRenderer(Boolean.class)));
table.setModel(new DefaultTableModel(new Object[][] {
{ "Row1", true, false, true },
{ "Row2", true, true, false },
{ "Row3", false, false, true },
{ "Row3", false, true, false } },
new String[] { "Header1", "Header2", "Header3", "Header4" }));
return table;
}
static class DelegatingTableCellEditor implements TableCellEditor
{
private final TableCellEditor delegate;
DelegatingTableCellEditor(TableCellEditor delegate)
{
this.delegate = delegate;
}
@Override
public Object getCellEditorValue()
{
return delegate.getCellEditorValue();
}
@Override
public boolean isCellEditable(EventObject anEvent)
{
return delegate.isCellEditable(anEvent);
}
@Override
public boolean shouldSelectCell(EventObject anEvent)
{
return delegate.shouldSelectCell(anEvent);
}
@Override
public boolean stopCellEditing()
{
return delegate.stopCellEditing();
}
@Override
public void cancelCellEditing()
{
delegate.cancelCellEditing();
}
@Override
public void addCellEditorListener(CellEditorListener l)
{
delegate.addCellEditorListener(l);
}
@Override
public void removeCellEditorListener(CellEditorListener l)
{
delegate.removeCellEditorListener(l);
}
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column)
{
Component component =
delegate.getTableCellEditorComponent(
table, value, isSelected, row, column);
if (component instanceof JComponent)
{
JComponent c = (JComponent)component;
c.setOpaque(true);
}
if (highlightCell(table, row, column))
{
component.setBackground(Color.CYAN);
}
else
{
component.setBackground(Color.WHITE);
}
return component;
}
}
static class DelegatingTableCellRenderer extends DefaultTableCellRenderer
{
private final TableCellRenderer delegate;
DelegatingTableCellRenderer(TableCellRenderer delegate)
{
this.delegate = delegate;
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
Component component =
delegate.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
if (component instanceof JComponent)
{
JComponent c = (JComponent)component;
c.setOpaque(true);
}
if (highlightCell(table, row, column))
{
component.setBackground(Color.CYAN);
}
else
{
component.setBackground(Color.WHITE);
}
return component;
}
}
private static boolean highlightCell(JTable table, int row, int column)
{
if (column == 1)
{
return false;
}
Object thisValue = table.getModel().getValueAt(row, column);
Object referenceValue = table.getModel().getValueAt(row, 1);
if (thisValue != null && referenceValue != null)
{
return thisValue.equals(referenceValue);
}
return false;
}
}