- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we implement a long text of the JOptionPane message dialog in Java?
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. 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. 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.
Example
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
- Related Articles
- How can we implement the HTML text of JButton in Java?
- How can we implement a moving text using a JLabel in Java?
- Can we set JOptionPane with predefined selection in Java?
- How can we implement a JLabel text with different color and font in Java?
- How can we implement a JToggleButton in Java?
- How can we implement line wrap and word wrap text inside a JTextArea in Java?
- How can we implement the paintComponent() method of a JPanel in Java?
- How can we implement a scrollable JPanel in Java?
- How can we implement a rounded JTextField in Java?
- How can we implement a Custom HashSet in Java?
- How can we implement a timer thread in Java?
- How can we implement a custom iterable in Java?
- How can we implement the word wrap JTableHeader of a JTable in Java?
- How can we implement editable JComboBox in Java?
- How can we implement transparent JDialog in Java?

Advertisements