问题描述
浏览 StackOverFlow 多年后,我开始问我的第一个问题!世界你好!
好的,让我们这样做。
-
我正在使用 NetBeans。
-
我有一个 JTable,并且使用 NetBeans 选项“自定义创建代码”,我编写了一个自定义 TableCellRenderer。这段代码:
new JTable() { public Component prepareRenderer(TableCellRenderer renderer,int Index_row,int Index_col) { // get the current row DefaultTableModel model = (DefaultTableModel) tablaNotas.getModel(); Component comp = super.prepareRenderer(renderer,Index_row,Index_col); if(Index_col != 0 && Index_col != 1 && model.getValueAt(Index_row,Index_col) != null && !model.getValueAt(Index_row,Index_col).equals("")){ if (Double.parseDouble(model.getValueAt(Index_row,Index_col).toString()) < 5.0 && !isCellSelected(Index_row,Index_col)) { comp.setForeground(opciones.getColorSuspensos()); } else if (!isCellSelected(Index_row,Index_col)) { comp.setForeground(opciones.getColorAprobados()); } } return comp;
} };
基本上,我是在告诉我的表,我希望所有 =5 涂成绿色。但我需要它跳过前两列。
这很好用。问题是当我开始填充 JTable 时,因为我覆盖了我已经在 JFrame 启动时调用的方法中提供的模型:
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("Apellidos");
tableModel.addColumn("Nombre");
for (i = 0; i < numPruebas; i++) {
tableModel.addColumn(contPruebas.getPruebasAsignatura().get(asignatura).get(trimestre).get(i).getEtiqueta());
}
tablaNotas.setModel(tableModel);
现在它也给我 0 和 1 列着色。 为什么?为什么它也需要前 2 列,即使我覆盖模型,仍然着色?
我正在重写模型,因为它在向模型添加列时抛出了这个 Arrayindexoutofboundsexception:
Exception in thread "AWT-EventQueue-0" java.lang.Arrayindexoutofboundsexception: 2
tablaNotas.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null,null},{null,null}
},new String [] {
"Apellidos","Nombre"
}
) {
Class[] types = new Class [] {
java.lang.String.class,java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
有什么我可以做的吗?我可以以任何方式重新覆盖 prepareRenderer 吗?我应该在代码中的哪个位置执行此操作?我有点失落。
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)