JOptionPane showOptionDialog Code Example

JOptionPane showOptionDialog Code Example

JOptionPane showOptionDialog Code Example
Unlike other JOptionPanes, i.e., showMessageDialog and showInputDialog, JOption showOptionDialog is pretty easy and straight forward.

 Simple JOption showOptionDialog

Execute below code.
[
import javax.swing.JOptionPane;

public class ShowOptionDialog {

    public static void main(String args[])
    {
        handleQuit();

    }
    public static void handleQuit()
    {
        int choice=JOptionPane.showOptionDialog(null, "Do you want to close system",
"Close System",JOptionPane.YES_NO_OPTION,null, null, null);
        if(choice==JOptionPane.YES_NO_OPTION)
        {
            System.exit(0);
        }
    }


}
]

Code Analysis

Above code does a simple task.Method handQuit() has an Option JOptionPane that pops up requesting user if to exit the program.If the user clicks YES option, the program is exited using System.exit(0) method

Adding ImageIcon to JOptionPane.showOptionDialog

[
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

/** * * @author acer */public class ShowOptionDialog {

    public static void main(String args[])
    {
        handleQuit();

    }
    public static void handleQuit()
    {
        int choice=JOptionPane.showOptionDialog(null, "Do you want to close system","Close System",
                JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,new ImageIcon(ShowOptionDialog.class.getResource("server.jpg")), null, null);
        if(choice==JOptionPane.YES_NO_OPTION)
        {
            System.exit(0);
        }
    }


}
]

Output

new ImageIcon(ShowOptionDialog.class.getResource("server.jpg")) code adds icon to the Option dialog 


How to control  JOptionPane showOptionDialog Prompts

Below code shows how you can control dialog options.

[
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

/** * * @author acer */public class ShowOptionDialog {

    public static void main(String args[])
    {
        handleQuit();

    }
    public static void handleQuit()
    {
        Object [] options={"Okay","Cancel"};
        int choice=JOptionPane.showOptionDialog(null, "Do you want to close system","Close System",
                JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,
new ImageIcon(ShowOptionDialog.class.getResource("server.jpg")), options, options[0]);
        if(choice==JOptionPane.YES_OPTION)
        {
            System.exit(0);
        }
    }


}
]
Above code remains the same.We have just introduced options.By setting options[0] ,Okay option is selected by default.Striking enter button will return Okay

See also:






COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: JOptionPane showOptionDialog Code Example
JOptionPane showOptionDialog Code Example
JOptionPane showOptionDialog Code Example
https://2.bp.blogspot.com/-I84LB3DIacc/WKSzEVm37xI/AAAAAAAAG5Y/gVrplh7U_XE9G8lDOoa6K92bRmZNeImVwCEw/s320/Java-showOptionDialog-min.PNG
https://2.bp.blogspot.com/-I84LB3DIacc/WKSzEVm37xI/AAAAAAAAG5Y/gVrplh7U_XE9G8lDOoa6K92bRmZNeImVwCEw/s72-c/Java-showOptionDialog-min.PNG
Java Codes
http://www.download-all.net/2017/02/joptionpane-showoptiondialog-code.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/02/joptionpane-showoptiondialog-code.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy