How can we implement a long text of the JOptionPane message dialog in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

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

raja
Published on 23-Jul-2019 15:16:02
Advertisements