Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Line numbers for text area

Posted on 2003-12-08
5
Medium Priority
?
343 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

604 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