java – jtable cellrenderer在运行时更改单元格的backgroundcolor
作者:互联网
我正在尝试创建一个表格和颜色特定的单元格,黄色,红色或白色,具体取决于其他列的内容.为此,我循环填充值的行,然后检查内容.对于当前显示在屏幕上的每一行都可以正常工作,但是当程序到达未显示的行时,或者如果用户尝试滚动每个单元格,则将其背景颜色更改为白色.我在网上搜索了解决方案,唯一合理的想法是在每次循环后重置cellRenderer,这不起作用,因为它也会重置每个单元格.
我希望有人知道这方面的解决方案,或者可以让我知道我在哪里搞砸了什么.
我正在使用这个循环
for(int e = 0; e < modules.size(); e++)
{
gui.clearOutputStream();
gui.getOutputStream().setText("Load Modul " + modules.get(e) + "\r\n");
version = getVersion(modules.get(e));
//Update current Row
updateRow(gui.getReleaseTabelle(), e);
}
它调用这种方法
public void updateRow(JTable target, int row){
//...
//insert Values here
//...
CustomRenderer cr = new CustomRenderer();
cr.tab = target;
if(!target.getValueAt(row, 2).equals(target.getValueAt(row, 3)))
{
cr.Val1 = target.getValueAt(row, 1).toString();
target.setValueAt("X", row, 1);
}
else if(!target.getValueAt(row, 7).equals(""))
{
cr.Val1 = target.getValueAt(row, 1).toString();
target.setValueAt("Y", row, 1);
}
else
{
}
target.getColumnModel().getColumn(1).setCellRenderer(cr);
}
这是我的CustomRenderer
class CustomRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 6703872492730589499L;
public String Val1;
public JTable tab;
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(tab.getValueAt(row, 1).equals("Y")){
cell.setBackground(new java.awt.Color(255, 255, 0));
tab.setValueAt(Val1, row, 1);
}
else if(tab.getValueAt(row, 1).equals("X")){
cell.setBackground(new java.awt.Color(255, 50, 50));
tab.setValueAt(Val1, row, 1);
}
else
{
//do nothing
}
return cell;
}
}
解决方法:
不要更新CutomRenderer类中的表数据. Ren Renrer类应检查条件并为单元格着色.我使用了CustomRenderer类,并根据单元格中的数据渲染单元格.如果单元格的数据为“Y”,则将其变为黄色.如果数据为“N”,则将其颜色设置为灰色.
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class ColoringCells {
private static Object[] columnName = {"Yes", "No"};
private static Object[][] data = {
{"Y", "N"},
{"N", "Y"},
{"Y", "N"}
};
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
JTable table = new JTable(data, columnName);
table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer());
table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer());
frame.add(new JScrollPane(table));
frame.setTitle("Rendering in JTable");
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
class CustomRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 6703872492730589499L;
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(table.getValueAt(row, column).equals("Y")){
cellComponent.setBackground(Color.YELLOW);
} else if(table.getValueAt(row, column).equals("N")){
cellComponent.setBackground(Color.GRAY);
}
return cellComponent;
}
}
标签:java,swing,jtable,background-color,tablecellrenderer 来源: https://codeday.me/bug/20190729/1569273.html