?
Solved

Line numbers for text area

Posted on 2003-12-08
5
Medium Priority
?
336 Views
Last Modified: 2010-08-05
How do i get the line numbers on the left side of text area which has a scroll bar. I am designing an IDE. I am using Java 2.1.2.
I have to use Java 2 1.2 only >please "don't" suggest java 1.4 solutions
0
Comment
Question by:neekris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9897305
This should do it:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;


public class Test2 extends JFrame {

      JTextArea jta = new JTextArea(),
                      line = new JTextArea();
      int lineNumber = 1;
      
      public Test2() {
                 
        JScrollPane scroller = new JScrollPane( jta,
                         ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                         ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
      
        scroller.setRowHeaderView( line );
        line.append( 1 + " 
" );
        
        Border border = BorderFactory.createEtchedBorder();
        JPanel jpb = new JPanel();
        jpb.setBorder( border );
        
        jta.addCaretListener( new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                          try {
                  int n = jta.getLineOfOffset( jta.getCaretPosition() ) + 1;
                     if ( n > lineNumber ) {
                       line.append( (lineNumber+1) + " 
" );
                               lineNumber++;
                             }
                      } catch (BadLocationException ble) {
                            System.out.println(" line exception! ");
                      }
                  }
     });
        
        scroller.setCorner( ScrollPaneConstants.LOWER_LEFT_CORNER, jpb );
      
        Container contentPane = getContentPane();
        addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e)
{                                                                                                                                                                                                                                    
                          System.exit(0);
         }
     });
       
        contentPane.add(scroller, BorderLayout.CENTER);            
            
      }
          
      public static void main( String args[] ) {
            
            Test2 test = new Test2();
            test.setBounds( 50, 50, 400, 400 );
            test.setVisible(true);
            
      }
      
}
0
 
LVL 35

Accepted Solution

by:
TimYates earned 1000 total points
ID: 9897309
SORRRY!!!!!!!!!!!


This:

----------------------

import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
import javax.swing.event.* ;
import javax.swing.text.* ;
import javax.swing.border.* ;

public class Test2
    extends JFrame
{

  JTextArea jta = new JTextArea(),
                  line = new JTextArea() ;
  int lineNumber = 1 ;

  public Test2()
  {

    JScrollPane scroller = new JScrollPane( jta,
                                            ScrollPaneConstants.
                                            VERTICAL_SCROLLBAR_ALWAYS,
                                            ScrollPaneConstants.
                                            HORIZONTAL_SCROLLBAR_ALWAYS ) ;

    scroller.setRowHeaderView( line ) ;
    line.append( 1 + "\n" ) ;

    Border border = BorderFactory.createEtchedBorder() ;
    JPanel jpb = new JPanel() ;
    jpb.setBorder( border ) ;

    jta.addCaretListener( new CaretListener()
    {
      public void caretUpdate( CaretEvent e )
      {
        try
        {
          int n = jta.getLineOfOffset( jta.getCaretPosition() ) + 1 ;
          if( n > lineNumber )
          {
            line.append( ( lineNumber + 1 ) + "\n" ) ;
            lineNumber++ ;
          }
        }
        catch( BadLocationException ble )
        {
          System.out.println( " line exception! " ) ;
        }
      }
    } ) ;

    scroller.setCorner( ScrollPaneConstants.LOWER_LEFT_CORNER, jpb ) ;

    Container contentPane = getContentPane() ;
    addWindowListener( new WindowAdapter()
    {
      public void windowClosing( WindowEvent e )
      {
        System.exit( 0 ) ;
      }
    } ) ;

    contentPane.add( scroller, BorderLayout.CENTER ) ;

  }

  public static void main( String args[] )
  {

    Test2 test = new Test2() ;
    test.setBounds( 50, 50, 400, 400 ) ;
    test.setVisible( true ) ;

  }

}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9897314
0
 

Author Comment

by:neekris
ID: 9902272
Good,But when i save the text area how do i get rid of the line numbers
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9902720
Just save jta...

the line numbers are in a seperate textarea, which is on the left of the jta text area (the main one)

Tim.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question