
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Long Text in JOptionPane Message Dialog in Java
When you have to show long text messages, you might find problems with the dialog becoming too big or the text getting cut off. In this article, we will learn to implement a long text of the JOptionPane message dialog in Java.
What is a JOptionPane?
A JOptionPane is a subclass of the JComponent class, which includes static methods for creating and customizing modal dialog boxes. A JOptionPane class can be used instead of a JDialog class to minimize the complexity of the code.
Syntax
The following is the syntax for JOptionPane initialization:
JOptionPane.showMessageDialog(null, "Tutorials Point");
The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.
The following are the two different approaches for implementing a long text of the JOptionPane message dialog in Java:
Using JTextArea with JScrollPane
By default, the JOptionPane message dialogs can support a single-line text, We can also implement a JOptionPane message dialog with a long text by customizing the JTextArea class using the JScrollPane.
The following are the steps for implementing a long text of the JOptionPane message dialog using JTextArea:
Button Creation & KeyListener
Creates a new JButton with the label "Show Dialog", and a key listener on the button that is triggered when an action is performed.
btn = new JButton("Show Dialog"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) {
Setting up JTextArea with wrapping
The JTextArea component generates a large text field with 5 rows and 15 columns called jta, and makes the text uneditable by setting the setEditable() as false.
JTextArea jta = new JTextArea(5, 15); jta.setText(msg); jta.setEditable(false);
Adding JScrollPane
Wraps the JTextArea in a scrollable container using the JScrollPane and displays the scrollable JTextArea inside a JOptionPane dialogue box.
JScrollPane jsp = new JScrollPane(jta); JOptionPane.showMessageDialog(null, jsp);
Example
Below is an example of implementing a long text of the JOptionPane message dialog using JTextArea:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JOptionPaneScrollTextMessage extends JFrame { private JButton btn; String msg; public JOptionPaneScrollTextMessage() { setTitle("JOptionPaneScrollTextMessage Test"); msg = " Java is a programming language that produces software for multiple platforms.\n When a programmer writes a Java application, the compiled code\n" + "(known as bytecode) runs on most operating systems (OS), including \n Windows, Linux and Mac OS. Java derives much of its syntax \n from the C and C++" + "programming languages.\n Java was developed in the mid-1990s by James A. Gosling, a former computer scientist with Sun Microsystems."; btn = new JButton("Show Dialog"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JTextArea jta = new JTextArea(5, 15); jta.setText(msg); jta.setEditable(false); JScrollPane jsp = new JScrollPane(jta); JOptionPane.showMessageDialog(null, jsp); } }); add(btn, BorderLayout.NORTH); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JOptionPaneScrollTextMessage(); } }
Output
Using HTML Formatting
JOptionPane supports HTML content, which allows for better text formatting. The following are the steps for implementing a long text of the JOptionPane message dialog using HTML Formatting:
Wrapping text in HTML
This wraps the message in an HTML div tag with a fixed width to enable line wrapping in the string named msg.
String msg = "<html><div style='width: 300px;'>" + // The text is put here "</div></html>";
Showing Text
It displays the HTML message in a popup dialog when the action is performed through JOptionPane.
public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null, msg); }
Example
Below is an example of implementing a long text of the JOptionPane message dialog using HTML Formatting:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JOptionPaneHTMLTextMessage extends JFrame { private JButton btn; String msg; public JOptionPaneHTMLTextMessage() { setTitle("JOptionPaneHTMLTextMessage Test"); msg = "<html><div style='width: 300px;'>" + "Java is a programming language that produces software for multiple platforms. " + "When a programmer writes a Java application, the compiled code " + "(known as bytecode) runs on most operating systems (OS), including " + "Windows, Linux and Mac OS. Java derives much of its syntax " + "from the C and C++ programming languages.<br><br>" + "Java was developed in the mid-1990s by James A. Gosling, " + "a former computer scientist with Sun Microsystems." + "</div></html>"; btn = new JButton("Show Dialog"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null, msg); } }); add(btn, BorderLayout.NORTH); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JOptionPaneHTMLTextMessage(); } }