gpt4 book ai didi

java - CustomTableModel + JTable = 表未填充

转载 作者:行者123 更新时间:2023-11-29 05:59:46 24 4
gpt4 key购买 nike

我正在尝试实现我自己的表模型以在 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 "";
}
}

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

附注我不确定是否有必要为填充的 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();
}

这是我所看到的表模型结构的图片:

enter image description here

我想从 timeIntervals 列表中填充黄色部分,从 days 列表中填充绿色部分,从 partialSchedule HashMap 中填充紫色部分。

最佳答案

  • 你的 AbstractTableModel缺少 important setXxx() method(s)

  • 为什么你用了太难的方法fireTableStructureChanged(); use proper fireXxxXxx()methods , 此代码示例包含 AbstractTableModel 的大部分重要方法使用适当的 fireXxxXxx()

  • 我会使用 DefaultTableModel基于 Vector<Vector<Object>>而不是 XxxMapXxxHashMap , 这种方式需要关于 JTable 的最深入的知识, XxxTableModelXxxMapXxxHashMap一起工作

关于java - CustomTableModel + JTable = 表未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10631956/

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