0

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

这是控制器类:

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);
}

}

第一个用户界面(表格):

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 的内容的问题。

4

0 回答 0