- 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
How can we display the line numbers inside a JTextArea in Java?
A JTextArea is a subclass of JTextComponent and it is a multi-line text component to display the text or allow the user to enter a text. A JTextArea can generate a CaretListener interface, which can listen to caret update events. By default, JTextArea does not display the line numbers, we have to customize the code by using a DocumentListener interface.
Example
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.Element; public class LineNumberTextAreaTest extends JFrame { private static JTextArea textArea; private static JTextArea lines; private JScrollPane jsp; public LineNumberTextAreaTest() { setTitle("LineNumberTextArea Test"); jsp = new JScrollPane(); textArea = new JTextArea(); lines = new JTextArea("1"); lines.setBackground(Color.LIGHT_GRAY); lines.setEditable(false); // Code to implement line numbers inside the JTextArea textArea.getDocument().addDocumentListener(new DocumentListener() { public String getText() { int caretPosition = textArea.getDocument().getLength(); Element root = textArea.getDocument().getDefaultRootElement(); String text = "1" + System.getProperty("line.separator"); for(int i = 2; i < root.getElementIndex(caretPosition) + 2; i++) { text += i + System.getProperty("line.separator"); } return text; } @Override public void changedUpdate(DocumentEvent de) { lines.setText(getText()); } @Override public void insertUpdate(DocumentEvent de) { lines.setText(getText()); } @Override public void removeUpdate(DocumentEvent de) { lines.setText(getText()); } }); jsp.getViewport().add(textArea); jsp.setRowHeaderView(lines); add(jsp); setSize(400, 275); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new LineNumberTextAreaTest(); } }
Output
- Related Articles
- How can we implement line wrap and word wrap text inside a JTextArea in Java?
- How to display a bold text inside the JTextArea in Java?\n
- How can we set the orientation of a JTextArea from right to left in Java?
- Java Program to display the contents in JTextArea
- How can we disable cut, copy and paste functionality of a JTextArea in Java?
- How can we disable the cell editing inside a JTable in Java?
- How to display JTextArea in the form of a table with GridLayout in Java?
- How can we limit the number of characters inside a JTextField in Java?
- How can I append text to JTextArea in Java?
- Can we select only some of the text in JTextArea?
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- Can we define a class inside a Java interface?
- Can we define an enum inside a class in Java?
- Can we define an enum inside a method in Java?
- Can we define an interface inside a Java class?

Advertisements