Solved

Line numbers for text area

Posted on 2003-12-08
5
289 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

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

Expert Comment

by:TimYates
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 75
topping3 challenge 14 48
wordlen challenge 3 46
wordmultiple challenge 12 87
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now