If you need a dialog in a Java GUI and you do not want to reinvent dialog hell then JOptionPane is your friend and occasional pest. It gives you message dialogs input dialogs confirm dialogs and full option dialogs with minimal fuss and maximum legacy charm. This guide covers the basics and a few practical tips so your Swing GUI behaves like it was tested by someone who actually uses a mouse.
JOptionPane lives in javax.swing so either import the class or call it with the full name. Swing dialogs are lightweight and fine for simple desktop tools. One small but very important rule is to run GUI work on the event dispatch thread so your windows do not behave like an angry sandwich.
import javax.swing.JOptionPane
or use
javax.swing.JOptionPane.showMessageDialog(null, "Hello")
showMessageDialog displays plain info warnings or errors. Use the message type to change the icon and the perceived severity. Works great for friendly confirmations and less great for announcing that the database is on fire.
JOptionPane.showMessageDialog(null, "Operation complete", "Done", JOptionPane.INFORMATION_MESSAGE)
Message types you will use often include INFORMATION_MESSAGE WARNING_MESSAGE ERROR_MESSAGE and PLAIN_MESSAGE. Use them to set expectations and avoid panic.
showInputDialog prompts the user for a string. Remember that cancel returns null so check for that before you try to parse numbers and summon exceptions.
String input = JOptionPane.showInputDialog(null, "Enter a number")
if (input == null) {
// user cancelled
} else {
try {
int n = Integer.parseInt(input)
// use n
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "That was not a number", "Input error", JOptionPane.ERROR_MESSAGE)
}
}
showConfirmDialog returns an integer constant such as JOptionPane.YES_OPTION so compare explicitly. This is where you decide whether to proceed or fake confidence while backing out.
int choice = JOptionPane.showConfirmDialog(null, "Proceed with dangerous action")
if (choice == JOptionPane.YES_OPTION) {
// go for it
} else if (choice == JOptionPane.NO_OPTION) {
// user said no
} else {
// cancelled or closed the dialog
}
showOptionDialog gives you custom labels icons and button ordering. Use it when the default Yes No Cancel does not capture the nuance of your app or when you want branded buttons that scream professionalism.
Object[] options = {"Save", "Discard", "Cancel"}
int sel = JOptionPane.showOptionDialog(null, "What do you want to do", "Confirm", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0])
if (sel == 0) {
// Save
} else if (sel == 1) {
// Discard
} else {
// Cancel or closed
}
You can pass an Icon to any dialog to swap out the default icon. Use an ImageIcon loaded from resources for light branding or a cheeky image when the app needs personality. You can also pass a parent component so the dialog centers on your window instead of floating off into the abyss.
Always start GUI code on the event dispatch thread with SwingUtilities.invokeLater so your dialogs do not race with window creation and ruin the user experience.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// create frame or show dialogs here
}
})
There you go. JOptionPane will not win any design awards but it will get the job done for many desktop tools. Use it for quick prompts and small utilities and then move on to nicer UIs when deadlines and dignity allow.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.