post-feature-image
HomejavaJOptionPane

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:






Name

android android Apps android emulators android.app antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment 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 node.js 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://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0RAhssHRMn3D_3OZgG52kQqzvOQedJnyng7e5QI_632aOaCp1B7DVd_tlGd2GFKUK7PMKecvhvuL8fmYG3_3b9Acxexpxr_xGOFlMRzP_esA3XF89AqTzZXAzg4z01NHU7ORkUKblI89d/s320/Java-showOptionDialog-min.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0RAhssHRMn3D_3OZgG52kQqzvOQedJnyng7e5QI_632aOaCp1B7DVd_tlGd2GFKUK7PMKecvhvuL8fmYG3_3b9Acxexpxr_xGOFlMRzP_esA3XF89AqTzZXAzg4z01NHU7ORkUKblI89d/s72-c/Java-showOptionDialog-min.PNG
Java Codes
https://download-all-stuff.blogspot.com/2017/02/joptionpane-showoptiondialog-code.html
https://download-all-stuff.blogspot.com/
http://download-all-stuff.blogspot.com/
http://download-all-stuff.blogspot.com/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