CustomTableModel + JTable =表格未填写

时间:2012-05-17 08:05:44

标签: java swing model-view-controller jtable tablemodel

我正在尝试实现自己的表模型以显示JTable中的计划。我实现了它,但表格甚至没有填充。也许我失去了一些需要实施的方法?这是我的代码:

public class ScheduleTableModel extends AbstractTableModel {

    private ArrayList<TimeInterval> timeIntervals;
    private ArrayList<Day> days;
    private LinkedHashMap<ScheduleSlot, Lesson> fullSchedule;
    private LinkedHashMap<ScheduleSlot, Lesson> partialSchedule;
    private ScheduleType scheduleType;


    public ScheduleTableModel(ArrayList<TimeInterval> timeIntervals, ArrayList<Day> days, LinkedHashMap<ScheduleSlot, Lesson> fullSchedule, LinkedHashMap<ScheduleSlot, Lesson> partialSchedule) {
        this.timeIntervals = timeIntervals;
        this.days = days;
        this.fullSchedule = fullSchedule;
        this.partialSchedule = partialSchedule;
        this.scheduleType = ScheduleType.PARTIAL;
        fireTableStructureChanged();
    }

    public ScheduleTableModel(LinkedHashMap<ScheduleSlot, Lesson> fullSchedule) {
        this.fullSchedule = fullSchedule;
        this.scheduleType = ScheduleType.GENERAL;
        fireTableStructureChanged();
    }

    @Override
    public int getRowCount() {
        if (scheduleType == ScheduleType.PARTIAL) {
            return timeIntervals.size() + 1;
        } else {
            return fullSchedule.size();
        }
    }

    @Override
    public int getColumnCount() {
        if (scheduleType == ScheduleType.PARTIAL) {
            return days.size() + 1;
        } else {
            return 3;
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (scheduleType == ScheduleType.PARTIAL) {
            if (columnIndex > 0 && rowIndex > 0) {
                for (Map.Entry<ScheduleSlot, Lesson> entry : partialSchedule.entrySet()) {
                    if (entry.getKey().getDay().equals(getColumnDay(columnIndex)) && entry.getKey().getTime().equals(getRowTimeInterval(rowIndex))) {
                        return entry;
                    }
                }
            } else if (rowIndex == 0 && columnIndex > 0) {
                return days.get(columnIndex);
            } else if (columnIndex == 0 && rowIndex > 0) {
                return timeIntervals.get(rowIndex);
            } else if (rowIndex == 0 && columnIndex == 0) {
                return "";
            }
        } else {
            List<Map.Entry<ScheduleSlot, Lesson>> scheduleIterator = new ArrayList<Map.Entry<ScheduleSlot, Lesson>>(fullSchedule.entrySet());
            switch (columnIndex) {
                case 0: {
                    scheduleIterator.get(columnIndex).getKey().getTimeSlot();
                }
                case 1: {
                    scheduleIterator.get(columnIndex).getKey().getPlaceSlot();
                }
                case 2: {
                    scheduleIterator.get(columnIndex).getValue();
                }
            }
        }
        return "";
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public Day getColumnDay(int columnIndex) {
        return (Day) getValueAt(0, columnIndex);
    }

    public TimeInterval getRowTimeInterval(int rowIndex) {
        return (TimeInterval) getValueAt(rowIndex, 0);
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        if (scheduleType == ScheduleType.GENERAL) {
            switch (columnIndex) {
                case 0: {
                    return fullSchedule.entrySet().iterator().next().getKey().getTimeSlot().getClass();
                }
                case 1: {
                    return fullSchedule.entrySet().iterator().next().getKey().getPlaceSlot().getClass();
                }
                case 2: {
                    return fullSchedule.entrySet().iterator().next().getValue().getGroup().getClass();
                }
                case 3: {
                    return fullSchedule.entrySet().iterator().next().getValue().getProfessor().getClass();
                }
                case 4: {
                    return fullSchedule.entrySet().iterator().next().getValue().getCourse().getClass();
                }

            }
        } else {
            return String.class;
        }
        return String.class;
    }

    @Override
    public String getColumnName(int column) {
        if (scheduleType == ScheduleType.GENERAL) {
            switch (column) {
                case 0: {
                    return "";
                }
                case 1: {
                    return "";
                }
                case 2: {
                    return "";
                }
                case 3: {
                    return "";
                }
                case 4: {
                    return "";
                }
            }
        }
        return "";
    }
}  

有人能指出我的错误吗?提前谢谢大家!

P.S。我不确定是否有必要为Jtable实现setValueAt。如果我是对的,你有必要展示一下这种方法的实现吗?

此外:

实现了setValueAt(),但没有改变:

@Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (scheduleType == ScheduleType.PARTIAL) {
            Map.Entry<ScheduleSlot, Lesson> entry = (Map.Entry<ScheduleSlot, Lesson>) aValue;
            if (columnIndex > 0 && rowIndex > 0) {
                Day day = getColumnDay(columnIndex);
                TimeInterval timeInterval = getRowTimeInterval(rowIndex);
                TimeInterval entryInterval = entry.getKey().getTime();
                Day entryDay = entry.getKey().getDay();
                if (day.equals(entryDay) && timeInterval.equals(entryInterval)) {
                    fullSchedule.put(new ScheduleSlot(new TimeSlot(entry.getKey().getTime(), entry.getKey().getDay()),
                            new PlaceSlot(entry.getKey().getRoom(), entry.getKey().getBuilding())), entry.getValue());
                }
            } else if (rowIndex == 0 && columnIndex > 0) {
                days.add((Day) aValue);
            } else if (columnIndex == 0 && rowIndex > 0) {
                timeIntervals.add((TimeInterval) aValue);
            }
        }

        fireTableDataChanged();
    }  

这是tablemodel结构的图片,我怎么看:

enter image description here

我想从timeIntervals列表填充黄色部分,绿色部分 - 来自days列表和purpule部分 - 来自partialSchedule hashmap。

1 个答案:

答案 0 :(得分:3)

  • 您的AbstractTableModel遗失important setXxx() method(s)

  • 您为什么使用过于艰难的方法fireTableStructureChanged(); use proper fireXxxXxx()methods,此代码示例包含AbstractTableModel的大多数重要方法,其中fireXxxXxx()

    < / LI>
  • 我将DefaultTableModel基于Vector<Vector<Object>>而不是XxxMapXxxHashMap,这种方式需要了解JTable,{ {1}}和XxxTableModelXxxMap一起工作