Java code to display line numbers in jtextarea

I am trying to write java code to pop up a text area, and if I add 9000 lines to jtextarea, it will display 9000 lines and a vertical line.

Anyway, can I do this? enter image description here

In any case, I can add a line number, for example, in pics.

Please help me! Thanks!!

Here is my code:

import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Font; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.ScrollPaneConstants; import javax.swing.text.DefaultCaret; public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final JFrame frame = new JFrame("Test"); JPanel panel = new JPanel(); panel.setLayout((LayoutManager) new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setOpaque(true); final JTextArea textArea = new JTextArea(20, 30); textArea.setWrapStyleWord(true); textArea.setEditable(true); textArea.setFont(Font.getFont(Font.SANS_SERIF)); JScrollPane scroller = new JScrollPane(textArea); scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); JPanel inputpanel = new JPanel(); inputpanel.setLayout(new FlowLayout()); JButton button = new JButton("Enter"); DefaultCaret caret = (DefaultCaret) textArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); panel.add(scroller); inputpanel.add(button); panel.add(inputpanel); frame.getContentPane().add(BorderLayout.CENTER, panel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); frame.setResizable(false); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame.dispose(); }}); frame.setSize(500, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 

Please help me! Thanks!!

+7
java swing jtextarea rowheader
source share
3 answers

See Text component number for a component that can be used as the title of a scroll bar that contains your text area.

+15
source share

Here's the code based on the post here , but fixed, for example. using efficient string concatenation to avoid lag while holding the ENTER key, for example.

 import javax.swing.*; import javax.swing.text.Element; import java.awt.*; public class LineNumberingTextArea extends JTextArea { private JTextArea textArea; public LineNumberingTextArea(JTextArea textArea) { this.textArea = textArea; setBackground(Color.LIGHT_GRAY); setEditable(false); } public void updateLineNumbers() { String lineNumbersText = getLineNumbersText(); setText(lineNumbersText); } private String getLineNumbersText() { int caretPosition = textArea.getDocument().getLength(); Element root = textArea.getDocument().getDefaultRootElement(); StringBuilder lineNumbersTextBuilder = new StringBuilder(); lineNumbersTextBuilder.append("1").append(System.lineSeparator()); for (int elementIndex = 2; elementIndex < root.getElementIndex(caretPosition) + 2; elementIndex++) { lineNumbersTextBuilder.append(elementIndex).append(System.lineSeparator()); } return lineNumbersTextBuilder.toString(); } } 

In your GUI class, just create an instance of LineNumberTextArea and assign it an instance of JTextArea to connect. Also set the JScrollPane rowHeaderView to an instance of LineNumberingTextArea :

 LineNumberingTextArea lineNumberingTextArea = new LineNumberingTextArea(myTextArea); myTextAreaScrollPane.setRowHeaderView(lineNumberingTextArea); 

Finally, add a DocumentListener for JTextArea update line numbers when the document changes:

 myTextArea.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent documentEvent) { lineNumberingTextArea.updateLineNumbers(); } @Override public void removeUpdate(DocumentEvent documentEvent) { lineNumberingTextArea.updateLineNumbers(); } @Override public void changedUpdate(DocumentEvent documentEvent) { lineNumberingTextArea.updateLineNumbers(); } }); 
+1
source share

You need something like this, maybe. Take the link from camickr.

Add the TextLineNumber class from this link to the working folder: Click here !

 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.border.EmptyBorder; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JTextArea; import javax.swing.JLabel; import javax.swing.LayoutStyle.ComponentPlacement; import java.awt.Font; public class TextLine extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TextLine frame = new TextLine(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public TextLine() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 540, 425); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JTextArea textArea = new JTextArea(); JScrollPane pane1 = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); TextLineNumber tln1 = new TextLineNumber(textArea); pane1.setRowHeaderView(tln1); contentPane.add(pane1, null); JLabel lblNewLabel = new JLabel(" Text Line Example"); lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18)); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup( gl_contentPane .createSequentialGroup() .addContainerGap(88, Short.MAX_VALUE) .addGroup( gl_contentPane .createParallelGroup(Alignment.LEADING) .addGroup( Alignment.TRAILING, gl_contentPane .createSequentialGroup() .addComponent(pane1, GroupLayout.PREFERRED_SIZE, 383, GroupLayout.PREFERRED_SIZE).addGap(43)) .addGroup( Alignment.TRAILING, gl_contentPane .createSequentialGroup() .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 333, GroupLayout.PREFERRED_SIZE).addGap(72))))); gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup( Alignment.TRAILING, gl_contentPane.createSequentialGroup() .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(pane1, GroupLayout.PREFERRED_SIZE, 278, GroupLayout.PREFERRED_SIZE).addGap(42))); contentPane.setLayout(gl_contentPane); } } 

Working class:

http://img28.imageshack.us/img28/3731/vm6p.png

-3
source share

All Articles