gpt4 book ai didi

java - 如何编写自定义 DefaultTableCellRenderer 为特定单元格着色, "preserve"为其他单元格着色,Java

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:51:05 25 4
gpt4 key购买 nike

我正在使用 netbeans 及其图形用户界面构建器来创建桌面应用程序。为了对我的 jTables 中的特殊单元格进行着色,我已经根据示例代码实现了自定义 DefaultTableCellRenderer。到目前为止它有效。

我的问题是覆盖函数“getTableCellRendererComponent()”的其他情况。我不希望不在指定范围内的单元格变成“白色”-我希望这些单元格以它们已有的颜色出现(例如,如果它们有,我不想将颜色更改为“白色”被“红色”)。

如果有人能帮助我,我会很高兴!!

非常感谢。
史蒂芬

class ColoredTableCellRenderer extends DefaultTableCellRenderer
{

/** constructor */
public ColoredTableCellRenderer(int rowToColorizeStart, int rowToColorizeStop, int columnToColorizeStart, int columnToColorizeStop, Color color)
{
mRowToColorizeStart = rowToColorizeStart;
mRowToColorizeStop = rowToColorizeStop;
mColumnToColorizeStart = columnToColorizeStart;
mColumnToColorizeStop = columnToColorizeStop;
mColor = color;
}

private int mRowToColorizeStart = 0;
public void setRowToColorizeStart(int rowToColorizeStart) {
mRowToColorizeStart = rowToColorizeStart;
}
public int getRowToColorizeStart() {
return mRowToColorizeStart;
}

private int mRowToColorizeStop = 0;
public void setRowToColorizeStop(int rowToColorizeStop) {
mRowToColorizeStop = rowToColorizeStop;
}
public int getRowToColorizeStop() {
return mRowToColorizeStop;
}

private int mColumnToColorizeStart = 0;
public void setColumnToColorizeStart(int columnToColorizeStart) {
mColumnToColorizeStart = columnToColorizeStart;
}
public int getColumnToColorizeStart() {
return mColumnToColorizeStart;
}

private int mColumnToColorizeStop = 0;
public void setColumnToColorizeStop(int columnToColorizeStop) {
mColumnToColorizeStop = columnToColorizeStop;
}
public int getColumnToColorizeStop() {
return mColumnToColorizeStop;
}

private Color mColor = Color.WHITE;
public void setColor(Color color) {
mColor = color;
}
public Color getColor() {
return mColor;
}


@Override
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( (row >= getRowToColorizeStart()) && (row <= getRowToColorizeStop()) && (column >= getColumnToColorizeStart()) && (column <= getColumnToColorizeStop()) && (isSelected == false) ) {
cellComponent.setBackground(getColor());
} else {
cellComponent.setBackground(Color.WHITE);
}

return cellComponent;

}
}

最佳答案

How to write a custom DefaultTableCellRenderer to colorize specific cells “only”, Java

代码行

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

谈论发生了什么

  • 单元格是否被选中(boolean isSelected)

  • 如果单元格被选中和/或也有焦点(boolean hasFocus)

  • 用于 JTable 中的行(int row)

  • 在 JTable 的列中(int 列)

然后你可以改变Font, Border, Color, Icon e.i.

注意 ---> 不要在 XxxRenderer 中更改 Object value,永远不要,一定要尽量避免这种情况

编辑

  • JTable 有两个维度,只定义了行,看看 prepareRenderer

  • prepareRenderer 指定用于行着色,my question can help youhere ,不要忘记重写 int modelRow = convertRowIndexToModel(row);,在大多数情况下,您可能会丢失索引模型而不是索引模型。 View ,因为 JTable 可以排序或过滤

关于java - 如何编写自定义 DefaultTableCellRenderer 为特定单元格着色, "preserve"为其他单元格着色,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12895283/

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