简体   繁体   English

JTable不会在fireXXXXX()事件上更新

[英]JTable doesn't update on fireXXXXX() events

there are some mistique things happening in my project. 我的项目中发生了一些模糊的事情。 Sorry, but here comes a huge part of code: 抱歉,这里有很大一部分代码:

public abstract class RowTableModel<T> extends AbstractTableModel {
protected List<T> modelData;
protected List<String> columnNames;
@SuppressWarnings("rawtypes")
protected Class[] columnClasses;
protected Boolean[] isColumnEditable;
@SuppressWarnings("rawtypes")
private Class rowClass = Object.class;
private boolean isModelEditable = true;

@SuppressWarnings("rawtypes")
protected RowTableModel(Class rowClass) {
    setRowClass(rowClass);
}

protected RowTableModel(List<String> columnNames) {
    this(new ArrayList<T>(), columnNames);
}

protected RowTableModel(List<T> modelData, List<String> columnNames) {
    setDataAndColumnNames(modelData, columnNames);
}

@SuppressWarnings("rawtypes")
protected RowTableModel(List<T> modelData, List<String> columnNames, Class rowClass) {
    setDataAndColumnNames(modelData, columnNames);
    setRowClass(rowClass);
}

protected void setDataAndColumnNames(List<T> modelData, List<String> columnNames) {
    this.modelData = modelData;
    this.columnNames = columnNames;
    columnClasses = new Class[getColumnCount()];
    isColumnEditable = new Boolean[getColumnCount()];
    fireTableStructureChanged();
}

@SuppressWarnings("rawtypes")
protected void setRowClass(Class rowClass) {
    this.rowClass = rowClass;
}

@SuppressWarnings({"rawtypes", "unchecked"})
public Class getColumnClass(int column) {
    // Class columnClass = null;
    //
    // if (column < columnClasses.length)
    // columnClass = columnClasses[column];
    //
    // // Get the default class
    //
    // if (columnClass == null)
    // columnClass = super.getColumnClass(column);
    //
    // return columnClass;
    Class columnClass = getValueAt(0, column).getClass();
    if (columnClass != null) {
        return columnClass;
    } else {
        return String.class;
    }

}
public int getColumnCount() {
    return columnNames.size();
}

public String getColumnName(int column) {
    Object columnName = null;

    if (column < columnNames.size()) {
        columnName = columnNames.get(column);
    }

    return (columnName == null) ? super.getColumnName(column) : columnName.toString();
}

public int getRowCount() {
    return modelData.size();
}

public boolean isCellEditable(int row, int column) {
    Boolean isEditable = null;

    // Check is column editability has been set

    if (column < isColumnEditable.length)
        isEditable = isColumnEditable[column];

    return (isEditable == null) ? isModelEditable : isEditable.booleanValue();
}

public void addRow(T rowData) {
    insertRow(getRowCount(), rowData);
}

public T getRow(int row) {
    return modelData.get(row);
}

@SuppressWarnings("unchecked")
public T[] getRowsAsArray(int... rows) {
    List<T> rowData = getRowsAsList(rows);
    T[] array = (T[]) Array.newInstance(rowClass, rowData.size());
    return (T[]) rowData.toArray(array);
}

public List<T> getRowsAsList(int... rows) {
    ArrayList<T> rowData = new ArrayList<T>(rows.length);

    for (int i = 0; i < rows.length; i++) {
        rowData.add(getRow(rows[i]));
    }

    return rowData;
}

public void insertRow(int row, T rowData) {
    modelData.add(row, rowData);
    fireTableRowsInserted(row, row);
}

public void insertRows(int row, List<T> rowList) {
    modelData.addAll(row, rowList);
    fireTableRowsInserted(row, row + rowList.size() - 1);
}

public void insertRows(int row, T[] rowArray) {
    List<T> rowList = new ArrayList<T>(rowArray.length);

    for (int i = 0; i < rowArray.length; i++) {
        rowList.add(rowArray[i]);
    }

    insertRows(row, rowList);
}

public void moveRow(int start, int end, int to) {
    if (start < 0) {
        String message = "Start index must be positive: " + start;
        throw new IllegalArgumentException(message);
    }

    if (end > getRowCount() - 1) {
        String message = "End index must be less than total rows: " + end;
        throw new IllegalArgumentException(message);
    }

    if (start > end) {
        String message = "Start index cannot be greater than end index";
        throw new IllegalArgumentException(message);
    }

    int rowsMoved = end - start + 1;

    if (to < 0 || to > getRowCount() - rowsMoved) {
        String message = "New destination row (" + to + ") is invalid";
        throw new IllegalArgumentException(message);
    }

    // Save references to the rows that are about to be moved

    ArrayList<T> temp = new ArrayList<T>(rowsMoved);

    for (int i = start; i < end + 1; i++) {
        temp.add(modelData.get(i));
    }

    // Remove the rows from the current location and add them back
    // at the specified new location

    modelData.subList(start, end + 1).clear();
    modelData.addAll(to, temp);

    // Determine the rows that need to be repainted to reflect the move

    int first;
    int last;

    if (to < start) {
        first = to;
        last = end;
    } else {
        first = start;
        last = to + end - start;
    }

    fireTableRowsUpdated(first, last);
}

public void removeRowRange(int start, int end) {
    modelData.subList(start, end + 1).clear();
    fireTableRowsDeleted(start, end);
}

public void removeRows(int... rows) {
    for (int i = rows.length - 1; i >= 0; i--) {
        int row = rows[i];
        modelData.remove(row);
        fireTableRowsDeleted(row, row);
    }
}

public void replaceRow(int row, T rowData) {
    modelData.set(row, rowData);
    fireTableRowsUpdated(row, row);
}

@SuppressWarnings("rawtypes")
public void setColumnClass(int column, Class columnClass) {
    columnClasses[column] = columnClass;
    fireTableRowsUpdated(0, getColumnCount() - 1);
}

public void setColumnEditable(int column, boolean isEditable) {
    isColumnEditable[column] = isEditable ? Boolean.TRUE : Boolean.FALSE;
}

public void setModelEditable(boolean isModelEditable) {
    this.isModelEditable = isModelEditable;
}

public static String formatColumnName(String columnName) {
    if (columnName.length() < 3)
        return columnName;

    StringBuffer buffer = new StringBuffer(columnName);
    boolean isPreviousLowerCase = false;

    for (int i = 1; i < buffer.length(); i++) {
        boolean isCurrentUpperCase = Character.isUpperCase(buffer.charAt(i));

        if (isCurrentUpperCase && isPreviousLowerCase) {
            buffer.insert(i, " ");
            i++;
        }

        isPreviousLowerCase = !isCurrentUpperCase;
    }

    return buffer.toString().replaceAll("_", " ");
}

} }

this is the abstract table model, which I extends like this: 这是抽象表模型,我将其扩展为:

public class ProfessorTableModel extends RowTableModel<Professor> {

public ProfessorTableModel(List<Professor> modelData,
        List<String> columnNames) {
    super(modelData, columnNames);
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Professor professor = super.getRow(rowIndex);
    switch (columnIndex) {
    case 0: {
        return professor.getId();
    }
    case 1: {
        return professor.getSurname();
    }
    case 2:{
        return professor.getName();
    }
    case 3:{
        return professor.getPatronymic();
    }
    case 4:{
        return professor.getMail();
    }
    case 5:{
        return professor.getPhone();
    }
    case 6:{
        return professor.getDays();
    }
    default:
        return null;
    }
}

@Override
public boolean isCellEditable(int row, int column) {
    return false;
}

and like this: 像这样:

public class LessonTableModel extends RowTableModel<Lesson>{
    public LessonTableModel(List<Lesson> modelData,
            List<String> columnNames) {
        super(modelData, columnNames);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Lesson lesson = super.getRow(rowIndex);
        switch (columnIndex) {
        case 0: {
            return lesson.getId();
        }
        case 1: {
            return lesson.getProfessor();
        }
        case 2:{
            return lesson.getCourse();
        }
        case 3:{
            return lesson.getGroup();
        }
        case 4:{
            return lesson.isLab();
        }
        default:
            return null;
        }
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
}

so in first case of models I make the following edit: 因此,在第一种情况下,我进行以下编辑:

int[] selectedIndexes = professorTable.getSelectedRows();
            if (selectedIndexes.length == 1) {
                int selectedIndex = professorTable.convertRowIndexToModel(professorTable.getSelectedRow());
                Professor professor = professorTableModel.getRow(selectedIndex);
                ProfessorFrame professorFrame = ProfessorFrame.show(professor);
                if (professorFrame.getDialogResult() == DialogResult.OK) {
                    professorTableModel.replaceRow(selectedIndex, professorFrame.getProfessor());
                }
            }   

and in second case I do almost the same: 在第二种情况下,我几乎可以做到:

int[] selectedIndexes = lessonTable.getSelectedRows();
                    if (selectedIndexes.length == 1) {
                        int selectedIndex = lessonTable.convertRowIndexToModel(lessonTable.getSelectedRow());
                        Lesson lesson = lessonTableModel.getRow(selectedIndex);
                        LessonFrame lessonFrame = LessonFrame.show(lesson);
                        if (lessonFrame.getDialogResult() == DialogResult.OK) {
                            lessonTableModel.replaceRow(selectedIndex, lessonFrame.getLesson());
                        }
                    }

But in first case the table get update correctly, and in second case the update is applied after I click the table. 但是在第一种情况下,表正确更新,而在第二种情况下,单击表后应用更新。 I understand that there is huge amount of code string, but I really hope that somebody can help. 我知道有大量的代码字符串,但我确实希望有人可以提供帮助。 (Tried replace fireTableRowInserted with fireTableDataChanged, but nothing was changed) (尝试将fireTableRowInserted替换为fireTableDataChanged,但未进行任何更改)

Thanks everybody in advance, I really appreciate your help! 在此先感谢大家,非常感谢您的帮助!

The problem was in the modality of the frames. 问题出在框架的形式上。 ProfessorFrame was modal and LessonFrame wasn't. ProfessorFrame是模态的,而LessonFrame不是。 That why the changes in first case apllied correctly cause the edited value was hooked, and in second case code was executed without pause (modality pause), and no new value was hooked. 这就是为什么在第一种情况下正确应用更改会导致钩住已编辑的值,而在第二种情况下为什么执行代码时没有暂停(模式暂停),也没有钩住新值。 Thanks @HovercraftFullOfEels for your help and for your suggestions. 感谢@HovercraftFullOfEels对您的帮助和建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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