好吧,我正在制作一个简单的程序来查看摆动元素的工作原理。主要问题是当我更改表模型的数据时不更新视图。我正在使用MVP,我尝试过“fireTableDataChanged”,“fireTableStructureChanged”,“重绘”,“验证”......
所以这是带有“registerStudent”方法的演示者:
public class StudentPresenter {
private List<Student> students;
private StudentView view;
public StudentPresenter(StudentView view) {
students = new ArrayList<>();
this.view = view;
}
public void run() {
JFrame frame = new JFrame("Students Registrator");
frame.setContentPane(view.getMainFrame());
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void registerStudent() {
students.add(new Student("Nuevo", 1, 2));
students.add(new Student("Otro", 3, 4));
((TableModel) view.getTable().getModel()).setData(students);
((TableModel) view.getTable().getModel()).refresh();
view.getTable().repaint();
}
}
使用“刷新”方法的TableModel:
public class TableModel extends AbstractTableModel {
private String[] columnNames = {"Nombre", "Edad", "Curso"};
private List<Student> data = new ArrayList<>();
public TableModel() {
data.add(new Student("Ejemplo", 0, 0));
}
public void setData(List<Student> data) {
this.data = data;
}
public void refresh() {
fireTableDataChanged();
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex) {
case 0:
return data.get(rowIndex).getName();
case 1:
return data.get(rowIndex).getAge();
case 2:
return data.get(rowIndex).getCourse();
default:
return null;
}
}
}
StudentView:
public class StudentView {
private StudentPresenter presenter;
private JMenuBar menuBar;
private JScrollPane scrollPane;
private JPanel mainFrame;
private JTable table;
private JButton registerButton;
public StudentView() {
createUIComponents();
}
private void createUIComponents() {
buildMenu();
buildButtons();
buildTable();
}
private void buildMenu() {
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Salir");
exitItem.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("Edit");
menuBar.add(fileMenu);
menuBar.add(editMenu);
}
private void buildButtons() {
registerButton = new JButton();
registerButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
presenter.registerStudent();
}
});
}
private void buildTable() {
table = new JTable(new TableModel());
table.setFillsViewportHeight(true);
table.setBackground(Color.LIGHT_GRAY);
table.setForeground(Color.gray);
table.setFont(new Font("Arial", Font.PLAIN, 12));
table.setRowHeight(30);
scrollPane = new JScrollPane(table);
}
public void setPresenter(StudentPresenter presenter) {
this.presenter = presenter;
}
public JPanel getMainFrame() {
return mainFrame;
}
public JTable getTable() {
return table;
}
}