gpt4 book ai didi

java - Swing——根据单元格的值设置单元格的颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:35 24 4
gpt4 key购买 nike

我想根据单元格的值设置单元格的颜色。在谷歌上搜索了一下,我发现我可以使用这样的方法来做到这一点:

public class TableCellRenderer extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int col)
{
// get the DefaultCellRenderer to give you the basic component
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
// apply your rules
if (value.toString().equals("Red"))
c.setBackground(Color.RED);
else
c.setBackground(Color.GRAY);
return c;
}
}

但我遇到的问题是,我想修改的代码已经为 JTable 的列设置了 TableCellRendererer。代码中有一个函数如下所示:

private void configureTableColumns() {      
Enumeration columns = this.table.getColumnModel().getColumns();

while (columns.hasMoreElements()) {
TableColumn tableColumn = (TableColumn) columns.nextElement();

this.setCellRenderer(tableColumn);
TableCellRenderer renderer = tableColumn.getCellRenderer();

if (renderer == null) {
renderer = this.table.getDefaultRenderer(Object.class);
}
tableColumn.setCellRenderer(renderer);
this.setCellEditor(tableColumn);
}
}

使用上面的代码,我还需要添加前面显示的 TableCellRenderer 类吗?我想要做的就是检查单元格的值是否为“ABC”并将背景设置为红色。

更新:

我确实尝试将我的 TableCellRenderer 版本添加为我要修改的代码中的内部类,但我收到一个错误,指出 tableColumn.getCellRenderer() 中存在类型不匹配。

Type mismatch: cannot convert from TableCellRenderer to MyTableExample.TableCellRenderer

谢谢

最佳答案

重写 prepareRenderer 方法。

例子:

public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){  
Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex);

if(getValueAt(rowIndex, columnIndex).toString().equals("Red")) {
componenet.setBackground(Color.RED);
} else if(getValueAt(rowIndex, columnIndex).toString().equals("Green")) {
componenet.setBackground(Color.GREEN);
}
return componenet;
}

更新:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;


public class PrepareRendereEx {

public static void main(String[] args) {

JFrame frame = new JFrame();
frame.setSize(new Dimension(400, 100));

Object data[][] = { {"java", "j2ee"},
{"java", "j2ee"},
{"java", "j2ee"}
};
Object[] columnNames = {"Java", "J2EE"};

DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
int columnIndex) {
JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);

if(getValueAt(rowIndex, 0).toString().equalsIgnoreCase("java") && columnIndex == 0) {
component.setBackground(Color.RED);
} else if(getValueAt(rowIndex, 1).toString().equalsIgnoreCase("j2ee") && columnIndex == 1){
component.setBackground(Color.GREEN);
}

return component;
}
};

frame.add(new JScrollPane(table));
frame.setVisible(true);
}
}

输出:

enter image description here

关于java - Swing——根据单元格的值设置单元格的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14425364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com