1

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)

Thanks everybody in advance, I really appreciate your help!

mr.nothing
  • 5,141
  • 10
  • 53
  • 77
  • 6
    If you don't get a decent solution here soon, consider debugging by continually whittling down your code, eliminating code stepwise from the program until you have isolated the problem or it goes away. If it goes away, likely the code just removed was involved. If isolated, then you can re-post a small easily understandable program that we can run, reproduce your problem, test the code, modify the code, and help you solve it. – Hovercraft Full Of Eels May 13 '12 at 13:56
  • I have to wonder what your `lessonFrame.getDialogResult()` method is doing. Therein lies the greatest difference between the two last paragraphs of code you posted. – Hovercraft Full Of Eels May 13 '12 at 14:52
  • lessonFrame.getDialogResult() returns the result of the dialog. IN other words it says if user clicked OK or Cancel. I'll try to cut this code to make it more cleaner. – mr.nothing May 13 '12 at 15:20
  • I understand what it returns, but I'm curious what's going on inside of that method, whether it's tying up the EDT or doing anything else that might prevent your GUI from behaving correctly. – Hovercraft Full Of Eels May 13 '12 at 15:43
  • Here's a working [example](http://stackoverflow.com/a/10491290/230513) using `fireTableCellUpdated()`. – trashgod May 13 '12 at 16:42
  • solved, I forgot to set the modality of lessonFrame to true and thats why all the code was executed without any pause. – mr.nothing May 14 '12 at 06:27

1 Answers1

0

The problem was in the modality of the frames. ProfessorFrame was modal and LessonFrame wasn't. 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.

mr.nothing
  • 5,141
  • 10
  • 53
  • 77