无法从另一个类更新AbstractTableModel

时间:2013-11-09 03:47:59

标签: java swing model-view-controller arraylist abstracttablemodel

我正在尝试更新从ArrayList中提取数据的JTable。我的程序中有两个框架。第一帧是JTable(AbstractTableModel),它显示ArrayList的内容。我单击该帧上的“新建”按钮以显示第二个窗口,这允许我添加到前面提到的ArrayList。当我单击“保存”按钮时,第二个窗口关闭,第一个窗口应该刷新新行。我的代码中没有任何语法错误,它在概念上看起来是正确的。我认为开始排除故障的第一个地方是NoteCntl类。我的印象是getNoteTableUI()应该在调用时使用新数据更新视图,但我对于发生了什么感到难过。我是模型视图控制器概念的新手,但我想尽可能地遵循它。

这是Controller类:

public class NoteCntl {

private NoteTableModel theNoteTableModel = new NoteTableModel();;
private NoteTableUI theNoteTableUI;
private NoteDetailUI theNoteDetailUI;

public NoteCntl(){
    theNoteTableUI = new NoteTableUI(this);
}

public NoteTableModel getNoteTableModel(){
    return theNoteTableModel;
}

public void getNoteDetailUI(Note theNote){
    if (theNoteDetailUI == null || theNote == null){
        theNoteDetailUI = new NoteDetailUI(this,theNote);
    }
    else{
        theNoteDetailUI.setVisible(true);
    }
}

public NoteTableUI getNoteTableUI(){
    theNoteTableModel.fireTableDataChanged();   //why doesn't this do anything?
    theNoteTableUI.setVisible(true);
    return theNoteTableUI;
}

public void deleteNote(int noteToDelete){
    theNoteTableModel.removeRow(noteToDelete);
}

}

第一个UI(表):

public class NoteTableUI extends JFrame{

NoteTableModel noteModel;
NoteCntl theNoteCntl;
JPanel buttonPanel;
JPanel tablePanel;
JTable theNoteTable;
JScrollPane theScrollPane;
JButton backButton;
JButton deleteButton;
JButton editButton;
JButton newButton;


public NoteTableUI(NoteCntl theParentNoteCntl){
    theNoteCntl = theParentNoteCntl;
    this.initComponents();
    this.setSize(400, 500);
    this.setLocationRelativeTo(null);
    this.setTitle("NoteTableUI");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}

public void initComponents(){
    buttonPanel = new JPanel();
    tablePanel = new JPanel();
    backButton = new JButton("Back");
    newButton = new JButton("New");
    newButton.addActionListener(new newButtonListener());

    editButton = new JButton("Edit");
    deleteButton = new JButton("Delete");
    deleteButton.addActionListener(new deleteButtonListener());
    noteModel = theNoteCntl.getNoteTableModel();
    theNoteTable = new JTable(theNoteCntl.getNoteTableModel());
    theScrollPane = new JScrollPane(theNoteTable);
    theNoteTable.setFillsViewportHeight(true);
    tablePanel.add(theScrollPane);

    buttonPanel.add(backButton);
    buttonPanel.add(deleteButton);
    buttonPanel.add(editButton);
    buttonPanel.add(newButton);

    this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
    this.getContentPane().add(tablePanel, BorderLayout.CENTER);
}

public class deleteButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        int selectedRow = theNoteTable.getSelectedRow();
        if (selectedRow == -1){
            System.out.println("No row selected");
        }
        else{
            noteModel.removeRow(selectedRow);
        }
        revalidate();
        repaint();
    }
}

public class newButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        NoteTableUI.this.setVisible(false);
        NoteTableUI.this.theNoteCntl.getNoteDetailUI(null);
        /*
        NoteDetailCntl theNoteDetailCntl = new NoteDetailCntl();
        lastRow++;
        long newRow = lastRow;
        noteModel.addRow(newRow, 0, "", "");
        revalidate();
        repaint();
        */
    }
}

第二个UI(细节编辑器) 公共类NoteDetailUI扩展了JFrame {

private final int FRAME_WIDTH = 700;
private final int FRAME_HEIGHT = 500;
private final int FIELD_WIDTH = 10;

JButton saveButton;
JButton backButton;
JTextField idField;
JTextField dateField;
JTextField nameField;
JTextField descriptionField;
JTextArea noteDetail;
JLabel idLabel;
JLabel dateLabel;
JLabel nameLabel;
JLabel descriptionLabel;
JPanel buttonPanel;
JPanel textFieldPanel;
JPanel textAreaPanel;
JPanel mainPanel;

NoteTableModel theNoteTableModel;
NoteDetailCntl theNoteDetailCntl;

NoteCntl theNoteCntl;
Note theCurrentNote;

public NoteDetailUI(){
    this.initComponents();
    this.setSize(FRAME_WIDTH,FRAME_HEIGHT);
    this.setLocationRelativeTo(null);
    this.setTitle("NoteDetailUI");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}

public NoteDetailUI(NoteCntl parentNoteCntl, Note theSelectedNote){
    theNoteCntl = parentNoteCntl;
    theCurrentNote = theSelectedNote;
    this.initComponents();
    this.setSize(400,500);
    this.setLocationRelativeTo(null);
    this.setTitle("Note");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}

public void initComponents(){
    saveButton = new JButton("Save");
    saveButton.addActionListener(new saveButtonListener());        
    backButton = new JButton("Back");
    backButton.addActionListener(new backButtonListener());

    idField = new JTextField(FIELD_WIDTH);

    theNoteTableModel = new NoteTableModel();

    idField.setText("10");
    idField.setEditable(false);
    dateField = new JTextField(FIELD_WIDTH);
    dateField.setText("20131108");
    nameField = new JTextField(FIELD_WIDTH);
    nameField.setText("Untitled");
    descriptionField = new JTextField(FIELD_WIDTH);
    descriptionField.setText("not described");

    idLabel = new JLabel("ID");
    dateLabel = new JLabel("Date");
    nameLabel = new JLabel("Name");
    descriptionLabel = new JLabel("Description");

    noteDetail = new JTextArea(25,60);

    buttonPanel = new JPanel();
    textFieldPanel = new JPanel();
    textAreaPanel = new JPanel();
    mainPanel = new JPanel(new BorderLayout());

    buttonPanel.add(backButton);
    buttonPanel.add(saveButton);
    textFieldPanel.add(idLabel);
    textFieldPanel.add(idField);
    textFieldPanel.add(dateLabel);
    textFieldPanel.add(dateField);
    textFieldPanel.add(nameLabel);
    textFieldPanel.add(nameField);
    textFieldPanel.add(descriptionLabel);
    textFieldPanel.add(descriptionField);
    textAreaPanel.add(noteDetail);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    mainPanel.add(textFieldPanel, BorderLayout.NORTH);
    mainPanel.add(textAreaPanel, BorderLayout.CENTER);

    add(mainPanel);

}

public ArrayList<String> getNoteDetails(){
    ArrayList<String> newData = new ArrayList<String>();
    newData.add(idField.getText());
    newData.add(dateField.getText());
    newData.add(nameField.getText());
    newData.add(descriptionField.getText());
    return newData;
}

public class saveButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        /*
         * Access the noteTableData array in NoteTableModel
         * Add the newData fields in order
         */
        if(theCurrentNote == null){
            int newNoteNumber = Integer.parseInt(NoteDetailUI.this.idField.getText());
            int newNoteDate = Integer.parseInt(NoteDetailUI.this.dateField.getText());
            String newNoteName = NoteDetailUI.this.nameField.getText();
            String newNoteDescription = NoteDetailUI.this.descriptionField.getText();

            NoteDetailUI.this.theCurrentNote = new EssayNote(newNoteNumber,newNoteDate,newNoteName,newNoteDescription);
            NoteDetailUI.this.setVisible(false);
            NoteDetailUI.this.dispose();
            NoteDetailUI.this.theNoteCntl.getNoteTableUI();
        }
        else{
            //if it's a current Note
        }
        //Refresh the JTable

    }
}

public class backButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent event){

    }
}

感谢所有帮助。如果你想运行程序并看看发生了什么,我可以提供其他类,但我怀疑它是控制器类中fireTableDataChanged()调用的问题,或者是更新SaveButtonListener中ArrayList内容的问题。

0 个答案:

没有答案