Solved

Line numbers for text area

Posted on 2003-12-08
5
321 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
  • 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 250 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
map related example 6 55
jmss example java 2 23
printf performancy 11 66
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

791 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