The Java JOptionPane Class
In a previous post I contrasted Java’s message dialog with the message box window that is available to .Net programmers. I also showed how each can be created with just one line of code. In this post my discussion will go a little further and we’ll see how much more useful the JOptionPane class can be to Java programmers.
The JOptionPane class is the class that provides the methods for creating a message dialog. Specifically, a message dialog window is an instance of the JOptionPane class. However, an instance of the JOptionPane class is not necessarily a message dialog window because the JOptionPane class can create several types of dialog windows through various public methods it exposes to the programmer. Those dialog windows can be the message dialog, an option dialog, a confirmation dialog, and an input dialog. In addition to those basic dialog windows, if you have special requirements you can also subclass the JOptionPane class to create your own custom dialog windows.
The Confirmation Dialog
![]()
The Input Dialog
Having these dialog windows ready made and available can be a convenience for the programmer because they provide a simple mechanism for displaying a temporary notification to the end user for purposes of information or input. In the JOptionPane class you will find three overloaded showMessageDialog methods that allow you to supply various parameters to effect a specific appearance and behavior from your message dialog. Additionally, several overloaded methods are provided for each of the other dialogs that can also be created from the JOptionPane class.
In this post I want to take a look at some of the ways we can specify how the lines of text are displayed in dialog windows especially regarding line breaks.
In a previous discussion, I stated that the Java message dialog will stretch horizontally to accommodate the length of the string you supply as the message parameter. If you want to control this behavior, you can include line breaks in your string. Line breaks will allow you to specify exactly where you want your text to end a line and start a new one. Many Java programmers are familiar with the new line escape code: “\n.” If you create a message dialog and use the escape code to force line breaks it may look something like this.
//
//Forcing multiple lines using the new line character: '\n'
JOptionPane.showMessageDialog(null, "First line." +
"\nSecond line." +
"\nThird line." +
"\nUsing \\n to force line breaks.");
//
On my system that code creates this window.

Using an escape code to force line breaks.
While this window looks like what I wanted it to look like, using the escape code as I have done here is not necessarily the proper way to write this code. In the Java specifications you will find that Sun, the makers of Java, recommend not using the newline character. The reason being that Java is a cross-platform technology meant to run across many environments and not all platforms may use the newline character to terminate a line.
Another option that would make for more portable code would be to retrieve the proper newline character for whatever environment the code is currently executing in. The Java framework provides the System class with a method for retrieving system properties. One of those properties allows you to determine the appropriate newline character to use in your code.
//
//Forcing multiple lines using the system property
//for 'line.separator'
String newLine = System.getProperty("line.separator");
JOptionPane.showMessageDialog(null, "First line." +
newLine + "Second line." +
newLine + "Third line." +
newLine + "Using the system 'line.separator' " +
"property to force line breaks.");
//
The preceding code retrieves the line separator property from the System class and uses it to set line breaks. The resulting window looks like this.

Using the GetProperty method of the System class to determine the proper escape code for line breaks.
In addition to the system line separator, another way to force line breaks is to pass an array of strings as the message parameter. Each element in the array will be displayed on it’s own line. If you execute this code:
//
//Forcing multiple lines using an array of strings as
//your message parameter.
String[] arrMsg = {"First line.",
"Second line.",
"Third line.",
"Using an array to force line breaks."};
JOptionPane.showMessageDialog(null, arrMsg);
//
The resulting window looks something like this:

Using an array of strings to set line breaks.
But wait, that’s not all. Another way to accomplish this task is to subclass the JOptionPane class and override the getMaxCharactersPerLineCount so that it returns the number of characters that you want to represent as the maximum for one line of text. However, subclassing is doing more than necessary when you have these other more simple methods to use as I have shown here. Typically, I would use the second example and retrieve the system property, unless the message was a dynamic string retrieved from a text file consisting of multiple lines. Then, if that is the case, and if you’re going to load the text into an array anyway, you could just use it as is.
I have only just scratched the surface on what you can accomplish using the JOptionPane class. There are ways to make a dialog display, in addition to plain text messages, a list of items in a listbox component or dropdown component, as well as other controls such as sliders. The fact that the message parameter is an Object type and not a string makes much of this possible. Furthermore, if you need dialogs that are more customized than anything you could accomplish with the JOptionPane class, you can subclass it and achieve an even greater degree of flexibility with your results.
Want to learn more?
Check out this page on Sun Microsystem’s website: How to Make Dialogs. That page is part of the Swing tutorials and it’s a must read if you want to know more about dialog windows in Java.
For even more on what you can do with the JOptionPane class, check out this page on the Java2s.com website for numerous examples (there were more than 40 examples the last time I visited).





paanu ma accept ng JOptionPane ang character???