gpt4 book ai didi

java - 当 setAutoCreateRowSorter 为真时刷新 JTable

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

我写了一个默认的表格渲染如下:

public class CustTableRenderer extends DefaultTableCellRenderer{

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
try {

Object cellObj = table.getModel().getValueAt(row, 7);
double cellValue = (Double) cellObj;

if (cellValue < 0) {
comp.setBackground(new Color(255, 48, 48));
} else if (cellValue == 0) {
comp.setBackground(new Color(173, 255, 47));
} else {
comp.setBackground(Color.white);
}

if (isSelected) {
comp.setBackground(new Color(71, 60, 139));
TableModel model = table.getModel();
}
} catch (Exception e) {
e.printStackTrace();
}
return comp;
}
}

为了突出显示包含第 7 列负值的行,我还将 setAutoCreateRowSorter 设置为 true。我的问题是,当我单击标题以根据它排序时,表格已排序但突出显示的行未更改,因此突出显示了错误的行。

表格排序后如何重绘?

最佳答案

传递给渲染器的坐标是在 View 坐标系中,你必须在访问模型之前将它们转换为模型坐标:

  int modelRow = table.convertRowIndexToModel(row);
int modelColumn = table.convertColumnIndexToModel(column);
cellObject = table.getModel().getValueAt(modelRow, modelColumn);

关于java - 当 setAutoCreateRowSorter 为真时刷新 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9392850/

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