gpt4 book ai didi

java - TableCellRenderer 选中单元格问题

转载 作者:行者123 更新时间:2023-11-30 09:50:36 25 4
gpt4 key购买 nike

我想实现一个 JTable 组件的 tablecellrenderer,它应该根据单元格数据显示不同的颜色。我明白了,但我无法更改所选单元格的颜色。我试着这样做:

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
{

if (isSelected) {
this.setBackground((Color)UIManager.get("Table.selectionBackground"));
this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
} else {
this.setForeground((Color)UIManager.get("Table.foreground"));
this.setBackground((Color)UIManager.get("Table.background"));
this.setBorder(BorderFactory.createEmptyBorder());
}
...
}

但它不起作用 :S .. 我看不出问题是因为当我单击一个单元格时 JTable 没有显示任何不同的内容。

最佳答案

I want to implement a tablecellrenderer of a JTable component, which should show a different color depending on the cell data

您发布的代码不会执行此操作。基本上你所有的代码都是复制渲染器的默认行为

您可能会找到 Table Row Rendering方法更易于实现。

关于java - TableCellRenderer 选中单元格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5098055/

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