- 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 −
Advertisements