在JTable中选择时,行数据未显示

时间:2014-03-13 10:59:08

标签: java jtable

我的JTable的最后一行有奇怪的问题。我无法选择该行,我只能选择该列。当我这样做时,列的数据没有显示。所有的行都变成了白色。我没有显示任何错误,因此我无法确定错误的位置。

这里是我如何创建JTable

table = new JTable(new StudentTableModel());
//create button to sort the table data
table.setAutoCreateRowSorter(true);
//use the costum rendrer for the table data
table.setDefaultRenderer(String.class, new StudentTableRenderer());
table.setDefaultRenderer(float.class, new StudentTableRenderer());
table.setDefaultEditor(float.class, new AverageCellEditor()); 

这里是JTable模型

public class StudentTableModel extends DefaultTableModel implements TableModel {

      public StudentTableModel(Model model, Teacher teacher, Classe classe, Lesson lesson) {
        super();
        if (model != null) {
        this.model = model;
        this.teacher = teacher;
        this.classe = classe;
        students = model.getStudentsInClass(classe.toString());
        this.lesson = lesson;
        Collections.sort(students, new PersonComparator());
        fireTableDataChanged();
    }
}


public int getColumnCount() {
    if (model == null)
        return 0;
    return names.length;
}

public String getColumnName(int col) {
    return names[col];
}

public int getRowCount() {
    if (model == null)
        return 0;
    return students.size();
}

public Object getValueAt(int line, int col) {
    switch (col) {
    case 0:
        return students.get(line).getLastName();
    case 1:
        return students.get(line).getFirstName();
    case 2:
        float note;
        if (lesson == null)
            note = students.get(line).getAverage().getMark();
        else
            note = students.get(line).getAverages().get(lesson).getMark();
        if (note == -1)
            return null;
        else
            return note;
    case 3:
        if (lesson == null)
            return students.get(line).getAverage().getComment();
        else
            students.get(line).getAverages().get(lesson).getComment();
    }
    return null;
}

public boolean isCellEditable(int row, int col) {
    if (col < 2)
        return false;
    if (lesson != null && teacher.getLesson().equals(lesson)) {
        return true;
    }
    if (lesson == null && classe.getResponsable().equals(teacher) && col == 3) {
        return true;
    }
    return false;
}

public void removeTableModelListener(TableModelListener arg0) {

}

public void setValueAt(Object object, int line, int col) {
    switch (col) {
    case 2:
        if (lesson == null)
            students.get(line).getAverage().setMark((float) object);
        else
            students.get(line).setAverage(lesson,(float) object);
        break;
    case 3:
        if (lesson == null)
            students.get(line).getAverage().setComment((String) object);
        else
            students.get(line).getAverages().get(lesson)
                    .setComment((String) object);
        break;
    }
    model.updatesTeachersAndStudents();
}

}

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了我的问题。 Table cell渲染器出现了问题。我总是将背景设置为白色,以便选择单元格,文本颜色变为白色,因此不会在白色背景上显示。