joptionpane

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.

A JOptionPane showOptionDialog example

The JOptionPane showOptionDialog is generally pretty straightforward, so without any introduction, here's the source code for a quick JOptionPane showOptionDialog example:

JOptionPane showInputDialog examples

I thought I'd share a collection of JOptionPane showInputDialog examples today. I'll start with the easiest example first, then try to add a little complexity as we go along.

First up, here's a simple JOptionPane showInputDialog example where we display a dialog that prompts a user to enter their name:

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 showConfirmDialog example

Here's some sample code that demonstrates how to use the Java JOptionPane showConfirmDialog method. I've included the method call in the context of a real containing method (doExitAction()) so you can see how it might be used in real-world code.

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