May 23, 2009 | Developer, HowTo

Java

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:

Java Swing MessageBox: PLAIN_MESSAGE

Java Swing MessageBox: PLAIN_MESSAGE

Java Swing MessageBox: INFORMATION_MESSAGE

Java Swing MessageBox: INFORMATION_MESSAGE

Java Swing MessageBox: QUESTION_MESSAGE

Java Swing MessageBox: QUESTION_MESSAGE

Java Swing MessageBox: WARNING_MESSAGE

Java Swing MessageBox: WARNING_MESSAGE

Java Swing MessageBox: ERROR_MESSAGE

Java Swing MessageBox: ERROR_MESSAGE

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.


If you liked this article

If you liked this article, don’t forget to subscribe for updates!

Subscribe to New Articles by RSS or E-mail

Get updates by RSS (What’s RSS?)

Subscribe by email:

Follow me on Twitter


Top Incoming Search Terms

"messagebox in java swing" +java +"message box" +swing +java +swing +"message box" +simple close jframe java messagebox dialog messagebox warning_message "java" display a message box in java equivalent to messagebox en java error messagebox in swings exibir dialogmessage no swing how to display a message box in java how to display message box in java how to display messagebox using java swing how to show a message box in java how to show messagebox java icon java messagebox java swing messagebox java contact me box java jframe messagebox java message box java messagebox java messagebox swing java msgbox java msgbox equivalent java show message box java show messagebox java swing message box java swing messagebox java swing msgbox java swing show message java swing show message box java swing show message in java java swing+messagebox meesagebox java mesagebox swing message box in java message box in java swing examples messagebox equivalent java messagebox equivalent swing messagebox in java messagebox in java swing messagebox in swing messagebox java swing messagebox swing messagebox using swing show a message box in java show contact email in java swings show error swing java show message box in java show message box in java using swing show message java swing show messagebox java show messagebox java swing simple message box in java swing mesagebox swing message box swing messagebox swing show message box what is swing message?

Related posts:

  1. How to Add an E-mail to a Gravatar Account Your Gravatar account can have more than one e-mail associated...
  2. How to Install Java on Windows Java is an application environment (like, in a way, .NET,...
  3. VLC Reverts to Normal Without the Santa Hat After New Year I wrote a few weeks ago about VLC sporting a...
  4. Sun to Create a Java App Store on the Lines of Apple’s iPhone Store Jonathon Schwartz, CEO of Sun Microsystems, blogs that Sun is...
  5. How to Claim Your Blog on Technorati Once you’ve signed up for a Technorati account, you should...

Explore related content: , , ,

3 Responses to “How to Show a MessageBox in Java Swing”

  1. Hello there. I discovered your blog by the use of Google while searching for a similar subject, your site came up. It appears great. I have bookmarked it in my google bookmarks to come back later.

  2. Today, I went to the beach front with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!

  3. Its like you learn my thoughts! You appear to grasp so much about this, such as you wrote the guide in it or something. I think that you just can do with some percent to power the message home a little bit, but other than that, that is fantastic blog. An excellent read. I’ll definitely be back.

Leave a Reply

CommentLuv Enabled