Java Program to display the contents in JTextArea

Java 8Object Oriented ProgrammingProgramming

The following is an example to display the contents of a text file in JTextArea −

Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingDemo {
   private JFrame mainFrame;
   private JLabel statusLabel;
   private JPanel controlPanel;
   public SwingDemo() {
      prepareGUI();
   }
   public static void main(String[] args) {
      SwingDemo demo = new SwingDemo();
      demo.showTextAreaDemo();
   }
   private void prepareGUI() {
      mainFrame = new JFrame("Java Swing");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });
      statusLabel = new JLabel("",JLabel.CENTER);
      statusLabel.setSize(350,100);
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);
   }
   private void showTextAreaDemo() {
      JLabel commentlabel= new JLabel("Text = ", JLabel.RIGHT);
      final JTextArea commentTextArea =
      new JTextArea("This is demo text!",5,20);
      JScrollPane scrollPane = new JScrollPane(commentTextArea);
      JButton showButton = new JButton("Show");
      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText( commentTextArea.getText());
         }
      });
      controlPanel.add(commentlabel);
      controlPanel.add(scrollPane);
      controlPanel.add(showButton);
      mainFrame.setVisible(true);
   }
}

Output

Click on the “Show” button above to display the text −

raja
Published on 08-May-2019 12:14:07
Advertisements