2013-05-10 17 views
0

我有一個框架,當用戶想要刪除一條記錄時,應該顯示一個警告窗格。認識到用戶在Joptionpane中選擇是或否

但是,現在我必須認識到,如果用戶選擇是,然後我刪除選定的行,如果選擇否,請不要刪除它!

怎麼樣?

if (e.getSource() == deleteUser) { 
JOptionPane.showConfirmDialog(rootPane, "Are You Sure To Delete?", "Delete User", WIDTH); 

// if yes, Then remove 
} 

回答

4

從JavaDoc中......

public static int showConfirmDialog(Component parentComponent, 
        Object message, 
        String title, 
        int optionType) 
          throws HeadlessException 

Brings up a dialog where the number of choices is determined by the optionType parameter. 

Parameters: 
    parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used 
    message - the Object to display 
    title - the title string for the dialog 
    optionType - an int designating the options available on the dialog: YES_NO_OPTION, YES_NO_CANCEL_OPTION, or OK_CANCEL_OPTION 
Returns: 
    an int indicating the option selected by the user 

返回類型將取決於你傳遞給optionType參數的值

這表明你應該做這樣的事情...

int result = JOptionPane.showConfirmDialog(rootPane, "Are You Sure To Delete?", "Delete User", JOptionPane.YES_NO_OPTION); 
if (result == JOptionPane.YES_OPTION) { 
    // Do something here 
} 

看看How to make dialogs for mor e細節...

+0

謝謝MadProgrammer ... – Sajad 2013-05-10 07:48:49

+1

請記住,JavaDocs和教程是你的朋友。如果您已經閱讀並且仍然卡住,您需要在您的問題中突出顯示這一點,我們會對您的努力給予更多的迴應;) – MadProgrammer 2013-05-10 11:59:33

相關問題