jtable未更新

oaxa6hgo  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(154)

我正在实现学生服务javaswing应用程序。我使用的是mvc模式。问题是当jdialog关闭时,表没有更新,但是新教授被添加到professordatabase类中的教授列表中。我什么都试过了,还没找到解决办法。下面是我的代码。
我已经创建了professortable(扩展了jtable)

public class ProfessorTable extends JTable{

public ProfessorTable() {

    setRowSelectionAllowed(true);
    setColumnSelectionAllowed(true);
    setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    setRowHeight(30);
    setModel(new AbstractProfessorTable());

    DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
    cellRenderer.setHorizontalAlignment(SwingConstants.CENTER);
    setDefaultRenderer(String.class, cellRenderer);

    JTableHeader tableHeader = this.getTableHeader();
    UIManager.put("TableHeader.cellBorder", BorderFactory.createMatteBorder(1, 1, 1, 1, new Color(58, 39, 93)));
    tableHeader.setPreferredSize(new Dimension(170, 25));
    tableHeader.setBackground(new Color(141, 102,217));
    tableHeader.setForeground(Color.white);    
    setGridColor(new Color(196,190,206));
    UIManager.put("Table.focusCellHighlightBorder", BorderFactory.createMatteBorder(1, 1, 1, 1, Color.white));

}

@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
    Component c = super.prepareRenderer(renderer, row, column);
    if (isRowSelected(row)) {
        c.setBackground(new Color(90, 90, 90));
        c.setForeground(Color.white);
    } else {
        c.setBackground(row % 2 == 0 ? Color.WHITE : new Color(232,232,232));
        c.setForeground(Color.black);
    }
    return c;
}

}
之后,我创建了一个abstractprofessortable类(扩展了abstracttablemodel)

public class AbstractProfessorTable extends AbstractTableModel{

public AbstractProfessorTable() {
}

@Override
public int getRowCount() {
    return ProfessorDatabase.getInstance().getProfessors().size();
}

@Override
public int getColumnCount() {
    return ProfessorDatabase.getInstance().getColumnCount();
}

    @Override
public String getColumnName(int column) {
    return ProfessorDatabase.getInstance().getColumnName(column);
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    return ProfessorDatabase.getInstance().getValueAt(rowIndex, columnIndex);
}

// For centering text in cell
@Override
public Class<?> getColumnClass(int column) {
    return getValueAt(0, column).getClass();
}

}
我有一个类被用作教授数据库Professor数据库:

public class ProfessorDatabase{

private static ProfessorDatabaseinstance = null;

private List<Proffesor> professors;
private List<String> columns;

private ProfessorDatabase() {
    initialize();
}

private void initialize() {

    initProfessors();

    this.columns = new ArrayList<String>();
    this.columns.add("Name");
    this.columns.add("Surname");
    this.columns.add("Title");
    this.columns.add("Academic title");
}

private void initProfesore() {
    this.professors = new ArrayList<Profesor>();
    professors.add(new Profesor("Mika", "Mikic", "Odod 12", "Inzenjer"));
    professors.add(new Profesor("Mika", "Mikic", "Odod 12", "Inzenjer"));
    professors.add(new Profesor("Mika", "Mikic", "Odod 12", "Inzenjer"));
}

public static ProfessorDatabase getInstance() {
    if(instance == null) {
        instance = new ProfessorDatabase();
    }
    return instance;
}

public List<Professor> getProfessors() {
    return professors;
}

public void setProfessors(List<Profesor> profesors) {
    this.professors = professors;
}

public int getColumnCount() {
    return 4;
}

public String getValueAt(int row, int column) {
    Professor professor = this.professors.get(row);
    switch (column) {
    case 0:
        return professor.getName();
    case 1:
        return professor.getSurname();
    case 2:
        return professor.getTitle();
    case 3:
        return professor.getAcademicTitle();
    default:
        return null;
    }
}

public String getColumnName(int index) {
    return this.columns.get(index);
}

public Profesor getRow(int rowIndex) {
    return this.professors.get(rowIndex);
}

public void addProfessor(String name, String surname, String title, String academicTitle) {
    professors.add(new Professor(name, surname, title, academicTitle));
}

}
我还有一个带卡片布局的jpanel(也有学生和成绩,但在不同的车上),显示这个表。带有前3位教授的表格以卡片布局显示在屏幕上。

public class TablePanel extends JPanel{

private static TablePanel instance = null;

public static TablePanel getInstance() {
    if (instance == null) {
        instance = new TablePanel();
    }
    return instance;
}

public static final String STUDENT_PANEL = "Studenti";
public static final String PROFESSOR_PANEL = "Profesori";
public static final String GRADE_PANEL = "Predmeti";
public static final String[] KEY_TEXTS = {STUDENT_PANEL, PROFESSOR_PANEL, GRADE_PANEL};
private CardLayout cardlayout = new CardLayout();
private JPanel cards = new JPanel(cardlayout);
private JTable professorTable;

private TablePanel() {
    StudentTable studentTable = new StudentTable();
    JScrollPane studentScrollPane = new JScrollPane(studentTable);
    studentScrollPane.setBorder(BorderFactory.createEmptyBorder());

    professorTable = new ProfessorTable();
    JScrollPane professorScrollPane = new JScrollPane(professorTable);
    professorScrollPane.setBorder(BorderFactory.createEmptyBorder());

    cards.add(studentScrollPane, STUDENT_PANEL);
    cards.add(professorScrollPane, PROFESSOR_PANEL);
    setLayout(new BorderLayout());
    add(cards, BorderLayout.CENTER);
    refreshView();
}

public void swapView(String key) {
       cardlayout.show(cards, key);
}

public void refreshView() {
    AbstractProfessorTable model = (AbstractProfessorTable) professorTable.getModel();
    model.fireTableDataChanged();
    validate();
}

}
我还有一个控制器可以更新模型和视图:

public class ProfessorController {

private static ProfessorController instance = null;

public static ProfessorController getInstance() {
    if (instance == null) {
        instance = new ProfessorController();
    }
    return instance;
}

private ProfessorController() {}

public void addProfessor(String name, String surname, String title, String academicTitle) {
    ProfessorDatabase.getInstance().addProfessor(name, surname, title, academicTitle);
    TablePanel.getInstance().refreshView();

}

}
通过jdialog调用一个方法来添加教授

ProfessorController.getInstance().addProfessor(textFieldList.get(0).getText(), textFieldList.get(1).getText(), textFieldList.get(2).getText(), textFieldList.get(3).getText());
                professorDialog.dispose();

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题