我正在尝试将prepareRenderer组件与自定义单元格渲染器一起使用。我们的想法是使用prepareRenderer绘制整行,并使用客户单元格渲染器绘制基于单元格的自定义。
prepareRenderer通过突出显示整行来按预期工作,但自定义单元格渲染器突出显示的单元格除非选中,否则不显示颜色。自定义单元格渲染似乎仅适用于单元格选择(突出显示包含1的单元格,但仅在选择时)。
关于如何让两者携手合作的任何想法?
以下代码重现问题。
[![import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import java.awt.*;
public class SortTableWithColors_ extends JFrame {
public static void main(String\[\] args) {
SortTableWithColors_ frame = new SortTableWithColors_();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public SortTableWithColors_() {
Object\[\] columnNames = {"B", "C"};
Object\[\]\[\] data = {{new Integer(1), new Integer(4)},
{new Integer(2), new Integer(5)},
{new Integer(3), new Integer(3)},
{new Integer(4), new Integer(1)}};
// table model
DefaultTableModel model = new DefaultTableModel(data, columnNames);
// set table model in Jtable
// JTable table = new JTable(model);
NewJTable table = new NewJTable(model);
table.setAutoCreateRowSorter(true);
getContentPane().add(new JScrollPane(table));
// Tell the table what to use to render our columns
for (int i = 0; i < 2; i++) {
table.getColumnModel().getColumn(i).setCellRenderer(new NewRenderer());
}
}
// Custom Renderer
public class NewRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
JLabel cell = (JLabel) super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
int rowModel = (int) table.convertRowIndexToModel(row);
int colModel = (int) table.convertColumnIndexToModel(column);
int rowView = (int) table.convertRowIndexToView(row);
int colView = (int) table.convertColumnIndexToView(column);
// set color
// cell.setBackground(new Color(0xFFFFFF));
// cell.setForeground(new Color(0x000000));
//set selection colors
if (isSelected) {
cell.setBackground(new Color(0x4AC3FF));
cell.setForeground(new Color(0x000000)); // AM
}
// Selective cell colouring based on value
// paint cells
int val = (int) value;
if (val == 1) {
cell.setBackground(Color.GREEN);
}
return cell;
}
}
public class NewJTable extends JTable {
public NewJTable(TableModel model) {
super(model);
}
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
// Color row based on a cell value
if (!isRowSelected(row)) {
c.setBackground(getBackground());
int modelRow = convertRowIndexToModel(row);
int val = (int) getModel().getValueAt(modelRow, 0);
if (val == 3) {
c.setBackground(Color.YELLOW);
}
}
return c;
}
}
}]
答案 0 :(得分:1)
不要试图一次做两件事。您需要在逻辑上将渲染分解为两个步骤。
渲染器代码在准备渲染器代码之前执行。所以:
首先,您需要让渲染器以您希望的方式工作。
然后,通过突出显示包含值3的行来覆盖默认渲染。
更改渲染器中的背景时的关键点是,在进行任何自定义之前,必须始终将其重置为默认值。
因此,在渲染代码中,您需要注释掉的行:
// set color (ie. restore the default values)
cell.setBackground(new Color(0xFFFFFF));
cell.setForeground(new Color(0x000000));
然后在prepareRenderer中,您不需要重置背景,只有在满足条件时才更改它:
// get rid of this, the default has already been set.
// this code should only provide the override
//c.setBackground(getBackground());
注意:只有在将背景重置为默认值的所有列上都有自定义渲染器时,此选项才有效。因为我们需要注释掉上面的语句,现在每个渲染器都有责任重置背景。