VisualBasic and a lot of other languages have a handy function to show a message box to the user. In typical Java fashion, Java’s equivalent has a lot more power, but also complex.
To get simple message box functionality, you have to specify a number of options:
JOptionPane.showMessageDialog(parent, "Message", "Title", JOptionPane.INFORMATION_MESSAGE, icon);
This shows a dialog with your message plus a look-and-feel defined icon which varies depending on the option selected. For example, a question mark icon is shown for JOptionPane.QUESTION_MESSAGE. The various message types are shown below for the default Swing Metal look-and-feel:
Note: the icon can be null if you don’t want or need an icon. You should specify a parent component (such as a JFrame) if you want the dialog to be modal with respect to that component. If you don’t mind that the user can click on something else and ignore your dialog message, leave it null.
If you want to get a response from the user as opposed to simply having him click OK, use the JOptionPane.showConfirmDialog() series of methods.
Related posts: