gpt4 book ai didi

java - 当我更改第 5 列的单元格时更改单元格中的颜色,他不会停在这里,他会更改下一个单元格

转载 作者:行者123 更新时间:2023-12-01 09:18:36 25 4
gpt4 key购买 nike

我在更改单元格中的颜色时遇到问题,当我更改第 5 列的单元格时,他并没有停在这里,他更改了下一个单元格...这是我的代码:

public class MyRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Etudiant E = new Etudiant();

if (column == 0) {
int id = E.getAIdEtud(table.getModel().getValueAt(row, 1).toString(), table.getModel().getValueAt(row, 2).toString());
if (E.IsRoudoublan(id) && E.IsExcclu(id)) {
c.setBackground(new java.awt.Color(0, 0, 255));
} else if (E.IsExcclu(id)) {
c.setBackground(new java.awt.Color(255, 0, 0));
} else if (E.IsRoudoublan(id)) {
c.setBackground(new java.awt.Color(20, 200, 0));
}
} else if (column == 5) {
if (Integer.parseInt(table.getModel().getValueAt(row, 5).toString()) >= 3) {
c.setBackground(new java.awt.Color(20, 200, 20));
}
} else {
c.setBackground(new java.awt.Color(100, 100, 100));
}

return c;

}
}

最佳答案

我拿走了你的代码片段并纠正了我发现的所有问题。检查所有带注释的行。

注释:

  • 为所有可能的代码路径提供颜色,您忘记在两个位置提供颜色(请参阅 // << provide ELSE part here, eg: 行)
  • TableCellRenderer.getTableCellRendererComponent 中报告的索引方法是 View 索引。
  • 为模型建立索引时,需要使用模型索引。您正在使用 View 索引来索引模型。这可能会导致问题,例如当您对表格进行排序时、当您重新排列列时或当您应用行筛选器时。查找末尾带有注释的行以进行更正。
<小时/>
public class MyRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowViewId, int columnViewId) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, rowViewId, columnViewId);
Etudiant E = new Etudiant();

int rowModelId = table.convertRowIndexToModel(rowViewId); // << for lookup of values in the model
int colModelId = table.convertColumnIndexToModel(columnViewId); // << for lookup of values in the model

if (colModelId == 0) { // << you mean to check if the model index is 0 here
int id = E.getAIdEtud(table.getModel().getValueAt(rowModelId, 1).toString(), table.getModel().getValueAt(rowModelId, 2).toString()); // << when indexing the model, use model indexes
if (E.IsRoudoublan(id) && E.IsExcclu(id)) {
c.setBackground(new java.awt.Color(0, 0, 255));
} else if (E.IsExcclu(id)) {
c.setBackground(new java.awt.Color(255, 0, 0));
} else if (E.IsRoudoublan(id)) {
c.setBackground(new java.awt.Color(20, 200, 0));
}
// << provide ELSE part here, eg:
else {
c.setBackground(new java.awt.Color(100, 100, 100));
}
} else if (colModelId == 5) { // << you mean to check if the model index is 5 here
if (Integer.parseInt(table.getModel().getValueAt(rowModelId, 5).toString()) >= 3) { // << when indexing the model, use model indexes
c.setBackground(new java.awt.Color(20, 200, 20));
}
// << provide ELSE part here, eg:
else {
c.setBackground(new java.awt.Color(100, 100, 100));
}
} else {
c.setBackground(new java.awt.Color(100, 100, 100));
}

return c;
}
}

关于java - 当我更改第 5 列的单元格时更改单元格中的颜色,他不会停在这里,他会更改下一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330737/

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