简体   繁体   中英

CustomTableModel + JTable = table isn't filled

I'm trying to implement my own table model to show the schedule in JTable. I implemented it, but table even isn't filled. Maybe I lost some methods which are required to be implemented? Here is my code:

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 "";
    }
}  

Could anybody point me on my mistakes? Thanks everyone in advance!

ps I'm not sure if it is necessary to implement setValueAt for Jtable be filled. If I'm right and it is necessary could you show an example of the implementation of this method?

ADDITION:

Implemented setValueAt(), but nothing changed:

@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();
    }  

Here is the picture of tablemodel structure how I see it:

在此输入图像描述

I wanna yellow part be filled from timeIntervals list, green part - from days list and the purpule part - from partialSchedule hashmap.

  • your AbstractTableModel missing important setXxx() method(s)

  • why did you use too hard methods fireTableStructureChanged(); use proper fireXxxXxx()methods , this code example contains most of important methods for AbstractTableModel with proper fireXxxXxx()

  • I'd be use DefaultTableModel based on Vector<Vector<Object>> rather than XxxMap or XxxHashMap , this way required depest knowledge about JTable , XxxTableModel and XxxMap or XxxHashMap works together

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM