2013-09-21 71 views
-1

我有一個表有一個用於添加新記錄的JDialog,當我單擊添加按鈕並想要添加一條新記錄並打開JDialog時,我關閉了JDialog窗口,並且它爲我所有列返回null我的表格行。當關閉JDialog時返回null

這是我的JDialog的構造:

public class AddBookDialog extends JDialog implements ActionListener { 

public AddBookDialog(JFrame owner) { 
    super(owner, "Add New Book", true); 
    initComponents(); 
    saveBtn.addActionListener(this); 
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
    setVisible(true); 
    } 

@Override 
public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == cancelBtn) dispose(); 
    else if (e.getSource() == saveBtn) saveAction(); 
     } 
} 
    public void saveAction() { 
    if (nameTf.getText().trim().length() != 0) { 
     if (!haveDigit(nameTf.getText().trim())) setBookName(nameTf.getText().trim()); 
     else { 
      JOptionPane.showMessageDialog(null, "Book Name have digit"); 
      return; 
     } 
    } else { 
     JOptionPane.showMessageDialog(null, "Enter Book Name"); 
     return; 
    } 
    if (isbnTf.getText().trim().length() != 0) { 
     if (haveSpace(isbnTf.getText().trim()) || haveLetter(isbnTf.getText().trim())) { 
      JOptionPane.showMessageDialog(null, "Enter Correct ISBN"); 
      return; 
     } 
     setIsbn(isbnTf.getText().trim()); 
    } else { 
     JOptionPane.showMessageDialog(null, "Enter Book ISBN"); 
     return; 
    } 

    setBorrowStatus("No"); 
    setDate(dateGenerate()); 
    dispose(); 
} 

我試着在我的表GUI類來控制這個問題:

public class BookPage_Admin extends JFrame implements ActionListener { 
... 

    public void addAction() { 
    AddBookDialog dialog = new AddBookDialog(this); 
    if (dialog.getBookName() != null && dialog.getIsbn() != null && dialog.getBorrowStatus() != null && 
      dialog.getDate() != null) { 
     Object[] added = new Object[]{dialog.getBookID(), dialog.getBookName(), dialog.getIsbn(), dialog.getBorrowStatus(), dialog.getDate()}; 
     model.addRow(added); 
    } 
    } 
} 

但仍當我關閉它,它返回null我行。

如何防止在關閉時返回null

+3

爲更好的幫助,儘早發佈[SSCCE](http://sscce.org/),簡短,可運行,可編譯,只是關於問題 – mKorbel

+0

驗證'model'不會影響表的模型。 – trashgod

+0

@trashgod你能解釋更多嗎? – Sajad

回答

1

以防萬一,如果沒有人會回答你:

當您啓動對話框,又名AddBookDialog dialog = new AddBookDialog(this);你可以在框架側像覆蓋ActionListener

AddBookDialog dialog = new AddBookDialog(this); 
dialog.setModal(true); 

dialog.getSaveBtn().addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     Object[] added = new Object[]{dialog.getBookID(), dialog.getBookName(), dialog.getIsbn(), dialog.getBorrowStatus(), dialog.getDate()}; 
     model.addRow(added); 

      } 
     }); 

// importent set visible after ActionListener!! 
dialog.setVisible(true); 

希望這將有助於,

+0

那麼,現在我應該在哪裏插入我的'saveAction()'? – Sajad

+0

您將偵聽器添加到'Save'按鈕,因此只需在'actionPerformed(ActionEvent e)'下調用它就像'dialog.saveAction()'' –