showmessagedialog

A Java JOptionPane showMessageDialog with scrolling text

Here's the source code for a simple JOptionPane showMessageDialog example, where I use a JTextArea inside of a JScrollPane to show a long text message in the showMessageDialog:

JOptionPane showMessageDialog examples (part 1)

JOptionPane showMessageDialog examples: I've been working with the Java JOptionPane showMessageDialog a lot lately, so I thought I'd create a page here with a number of JOptionPane showMessageDialog examples, sort of a JOptionPane reference page.

I'll walk you through some JOptionPaneexamples here, starting simple, and increasing the level of difficulty as we go on.

A collection of JOptionPane examples and tutorials

As I continue my efforts to organize the content on this site, here is a collection of JOptionPane tutorials and examples.

JOptionPane showMessageDialog examples (part 2)

With custom icon

It's important to note that you can also customize the icon that is displayed when using a JOptionPane showMessageDialog. In the following example, I specify that I want to display my own PNG image by creating an ImageIcon as an additional argument to the showMessageDialog method:

JOptionPane showMessageDialog component example

As I mentioned in my JOptionPane showMessageDialog examples, you can easily create a JOptionPane dialog that shows a Java component. The component you show can be a JPanel a JScrollPane JLabel and many more things, anything that extends JComponent.

JOptionPane showMessageDialog - a Java class to show long messages

Some times when exceptions occur when users are running a Java JFC/Swing application, it's nice to be able to show them a long message about what happened, i.e., something more than a one-line message. To that end I created a really simple Error Message Panel (ErrorMessagePanel.java) that you can use with the JOptionPane to display long messages.

Syndicate content