Solved

Line numbers for text area

Posted on 2003-12-08
5
326 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

749 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