- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Java Program to display the contents in JTextArea
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 −
- Related Articles
- How to display a bold text inside the JTextArea in Java?\n
- Java Program to replace the first 10 characters in JTextArea
- How to display JTextArea in the form of a table with GridLayout in Java?
- Java Program to paste clipboard text to JTextArea
- Java Program to set JTextArea to wrap by word in Java
- How can we display the line numbers inside a JTextArea in Java?
- Program to get JTextArea font information
- Java program to convert the contents of a Map to list
- Display the contents of a VIEW in MySQL?
- Java program to merge contents of all the files in a directory
- How to get the Tab Size of a JTextArea in Java?
- How can I append text to JTextArea in Java?
- Delete the first 10 characters from JTextArea in Java
- Java Program to Create String from Contents of a File
- Java Program to display JTabbedPane on the right

Advertisements