How to implement Line number in JTextArea

Hi
I have seen some JTextArea with Line numbers How to do it
Thankx

I'm playing around with trying to make a Line Number component that will work with JTextArea, JTextPane, JTable etc. I'm not there yet, but this version works pretty good with JTextArea. At least it will give you some ideas.
Good luck.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;
public class LineNumber extends JComponent
     private final static Color DEFAULT_BACKGROUND = new Color(230, 163, 4);
     private final static Color DEFAULT_FOREGROUND = Color.black;
     private final static Font DEFAULT_FONT = new Font("monospaced", Font.PLAIN, 12);
     // LineNumber height (abends when I use MAX_VALUE)
     private final static int HEIGHT = Integer.MAX_VALUE - 1000000;
     // Set right/left margin
     private final static int MARGIN = 5;
     // Line height of this LineNumber component
     private int lineHeight;
     // Line height of this LineNumber component
     private int fontLineHeight;
     private int currentRowWidth;
     // Metrics of this LineNumber component
     private FontMetrics fontMetrics;
     *     Convenience constructor for Text Components
     public LineNumber(JComponent component)
          if (component == null)
               setBackground( DEFAULT_BACKGROUND );
               setForeground( DEFAULT_FOREGROUND );
               setFont( DEFAULT_FONT );
          else
               setBackground( DEFAULT_BACKGROUND );
               setForeground( component.getForeground() );
               setFont( component.getFont() );
          setPreferredSize( 9999 );
     public void setPreferredSize(int row)
          int width = fontMetrics.stringWidth( String.valueOf(row) );
          if (currentRowWidth < width)
               currentRowWidth = width;
               setPreferredSize( new Dimension(2 * MARGIN + width, HEIGHT) );
     public void setFont(Font font)
          super.setFont(font);
          fontMetrics = getFontMetrics( getFont() );
          fontLineHeight = fontMetrics.getHeight();
     * The line height defaults to the line height of the font for this
     * component. The line height can be overridden by setting it to a
     * positive non-zero value.
     public int getLineHeight()
          if (lineHeight == 0)
               return fontLineHeight;
          else
               return lineHeight;
     public void setLineHeight(int lineHeight)
          if (lineHeight > 0)
               this.lineHeight = lineHeight;
     public int getStartOffset()
          return 4;
     public void paintComponent(Graphics g)
          int lineHeight = getLineHeight();
          int startOffset = getStartOffset();
          Rectangle drawHere = g.getClipBounds();
//          System.out.println( drawHere );
// Paint the background
g.setColor( getBackground() );
g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);
          // Determine the number of lines to draw in the foreground.
g.setColor( getForeground() );
          int startLineNumber = (drawHere.y / lineHeight) + 1;
          int endLineNumber = startLineNumber + (drawHere.height / lineHeight);
          int start = (drawHere.y / lineHeight) * lineHeight + lineHeight - startOffset;
//          System.out.println( startLineNumber + " : " + endLineNumber + " : " + start );
          for (int i = startLineNumber; i <= endLineNumber; i++)
               String lineNumber = String.valueOf(i);
               int width = fontMetrics.stringWidth( lineNumber );
               g.drawString(lineNumber, MARGIN + currentRowWidth - width, start);
               start += lineHeight;
          setPreferredSize( endLineNumber );
     public static void main(String[] args)
          JFrame frame = new JFrame("LineNumberDemo");
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          JPanel panel = new JPanel();
          frame.setContentPane( panel );
          panel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
          panel.setLayout(new BorderLayout());
          JTextArea textPane = new JTextArea();
          JScrollPane scrollPane = new JScrollPane(textPane);
          panel.add(scrollPane);
          scrollPane.setPreferredSize(new Dimension(300, 250));
          LineNumber lineNumber = new LineNumber( textPane );
          lineNumber.setPreferredSize(99999);
          scrollPane.setRowHeaderView( lineNumber );
          frame.pack();
          frame.setVisible(true);

Similar Messages

  • How can we get the selected line number from JTextArea ?

    how can we get the selected line number from JTextArea ? and want to insert line/string given line number into JTextArea??? is it possible ?

    Praitheesh wrote:
    how can we get the selected line number from JTextArea ?
    textArea.getLineOfOffset(textArea.getCaretPosition());
    and want to insert line/string given line number into JTextArea??? is it possible ?
    int lineToInsertAt = 5; // Whatever you want.
    int offs = textArea.getLineStartOffset(lineToInsertAt);
    textArea.insert("Text to insert", offs);

  • How to insert line number into APPLIED_CUSTOMER_TRX_LINE_ID?

    Hi All,
    I am working on the table of receivable AR_RECEIVABLE_APPLICATIONS_ALL.
    I get some information about APPLIED_CUSTOMER_TRX_LINE_ID form TRM.
    APPLIED_CUSTOMER_TRX_LINE_ID     NUMBER     (15)     
    The line number of the debit item or credit memo to which a payment or credit memo is applied
    So, I want to use APPLIED_CUSTOMER_TRX_LINE_ID to join with transaction's line (RA_CUSTOMER_TRX_LINES_ALL).
    But after applied the credit memo, the APPLIED_CUSTOMER_TRX_LINE_ID is NULL.
    How to insert line number into APPLIED_CUSTOMER_TRX_LINE_ID?

    Hi Eric,
    The application is already at the AR Invoice (header) level. Oracle doesn't give you privilege to apply the invoice at invoice line or distribution level. You need to match the applied_customer_trx_id to customer_trx_id of ra_customer_trx_all to get the applied invoice/debit memo number. Hope my comments are helpful to you.
    KG

  • How to read line number text from PDF using plugin?

    Hi, I would like to know how to read line number text from PDF using plugin?
    Thanks in advance.

    Ok, some background reading of the PDF Reference will help you understand why this is so difficult. PDF files are not organised into lines. It is best to think of each word or character on the page as being a graphic with its own position. The human eye sees lines where a series of graphics (words) are roughly in the same horizontal region.
    In the general case it is difficult or even impossible to answer this. You may have columns with different spacing (but the PDF stores no information on what is a column). You may have subscripts and superscripts. You may have text in graphics coinciding with other text. Commonly, there may be titles, headings or page numbers which are just ordinary text and might count as lines.
    That said, what you need to do is extract the text on the page and its positions. The WordFinder APIs are the way to do that. Now, sort all the words out, using the Y coordinates and size to try and guess what makes a "line". Now you are in a position to find the text (divided into words, not strings) and report the "line number" you have estimated.

  • How to get Line Number in Purchase Order

    Hi Experts
    I want to get Line Number of Matrix of Purchase Order. How I get it ?
    I want to compare Matrix Line Number with Line Number of POR1 in DB.
    Please Help me.
    Hepil Doshi

    Hi Hepil,
    Linenum in Matrix "38" is Columns.Item("110").Value
    SBO application doesnot show it, but you can get the linenum from that column.
    You can test by getting the column title. omatrix.columns.item("110").Title
    Hope it helps.
    Regards
    Edy

  • How to determine line number on Table control in BDC

    Hi,
    I am uploading changes for CG02 using BDC. I have to change data to specification type which are listed in a table control having some of the fields as IDCAT, IDTYPE, LANG, ORD, IDENT etc.
    For a particular substance number, there can be variable number of idtypes. Since there were 8 Idtypes already created for the substance number I was working with while recording and I created 9th Idtyep(Z_SARADOT#) while recording (Using SHBD), I am getting the code like ...'ESTRI-IDTYPE(09)....ESTRI-IDCAT(09).....'RCGRIIOT-IDENT(09)' which indicate that while recording, the values were added on 9th line of table control.
    Now this line number can vary for other substance number. How can I know
    (1) The last line of table control
    (2) Line number based on Value of field
        Example - here I want to know line number corresponding to IDTYPE Z_SARADOT# if it was created so that I can change the value of field IDENT of the table control.
    The piece of code I got after recording is:
              perform bdc_field       using 'ESTRI-IDTYPE(09)'
                                            'NUM'.
              perform bdc_field       using 'ESTRI-IDCAT(09)'
                                            'Z_SARADOT#'.
              perform bdc_field       using 'RCGRIIOT-IDENT(09)'
                                            '1175'.     
    Please help friends.
    Thanks & Regards,
    Surya

    There are around 800 Substance numbers and for each substance number there are around 10 IDtype. So It would be a costly processing.
    If there no other way doing it?
    I think, I need to follow this way as We can not determine the line number untill BDC is running.

  • How to implement line numbers in order acknowledge report

    Hi all,
            I am newbie to Oracle Reports.
    I have an order acknowledgement report in which i show columns like
    line number
    Ordered item
    ship from
    quantity
    unit price
    extended price
    In above table format i need to show line numbers as 1.1
                                                                                  1.2
                                                                                  1.3....                        
                                                                                   2.1
                                                                                   2.2
                                                                                   2.3....
    so, how can i achieve this functionality
    Thanks

    That still doesn't say much. How do you need to get from line number in your table to line number on the report? Give an example of your data.
    I can give you an example:
    with line_numbers as (
      select 11 lineno from dual
      union all
      select 12 lineno from dual
      union all
      select 13 lineno from dual
      union all
      select 21 lineno from dual
    select lineno /10 lineno
    from line_numbers
    order by lineno
    LINENO
    1.1
    1.2
    1.3
    2.1

  • How to assign Line Number for Lines when click on Add More Lines

    Hello All,
    Kindly help me to assign line number for the lines which are added through Add More Lines button on an Advance table. If user clicks on the button Add More Lines then there are 5 lines get inserted at a time but I am not able to handle the line number for them.
    Similarly, if user delete any of the line then I need to re-order them.
    Kindly help and share if there is any sample code.
    Thanks,
    Sandy

    Hi,
    Check if this link helps you :
    Re: How to display serial numbers in the results table region
    --Sushant                                                                                                                                                                                                                                                           

  • How to get Line number from a DOM Element

    Hi
    I am using dom xerces parser. Is there anyway to work out which line number a element will be when it is outputted into a file ??

    org.w3c.dom.DOMLocator method getLineNumber() returns the line number at which an error occurs.
    A corrresponding method for file output is not defined.

  • How to get line number in JAVA code?

    Hi,
    What method can I use to return the current line number of my coding?
    Assuming I'm at line 10 and I want to do something like System.out.println("I am at line number " + ????);What method should I use to get "10"?
    Please advise.
    Thank you.

    If you run Java in Dos prompt, a new line of result is displayed at bottom and the screen scroll. There is no line number in that way. Other language like C have something like that.
    If you use Java Swing, you can display with x, y coordinate option but it is graphic mode.

  • How to implement line selectability for a table control using table Wizard?

    Hello SDN Community,
    I have created a table control using the Table Wizard.  I found my exact question in this forum, but unfortunately it had not been andsered.  While I cannot paste a screen-print into this plane-text area, here are the steps I followed...
    1) SE51
    2) Create new screen 0100
    3) Click Layout button
    4) Clidk Table Control (with Wizard) and draw box on canvas.
    5) Step is "Start" - click Continue
    6) Step is "Name of Table Control" - provided name
    7) Step is "Table Name" - provided name of dictionary table (AUFK)
    8) Step is "Definition of Columns" - selected order numver and order text
    9) Step is "Table Control Attributes" - Line Selectability is in display mode - cannot set it.
    I would like to have a selectability column for my table.  Would appreciate any insight into how to do this.
    Thank you,
    Dean Atteberry.

    This is a puzzling...
    For the table control wizard, in the Table Control Attributes step, I was able to get line selectability to open up by declaring a char01 data element at the beginning of my type.
    The puzzling is in regards to the "Selection col. fld" entry field.
    If I leave it blank and try to go to the next step, I get message "Enter the name of the selection column if you are using a program table"
    So it looked like it wanted to know the name of my selection column.  So I type in "CHAR1".  and got the message "The field "CHAR1" for the selection column is already contained in the table."
    Hmmmmm.... don't understand............
    Dean Atteberry.

  • Web ADI GL Journal Template - Journal Line Number

    Is anyone know if you can add the journal line number to a web adi journal template.
    Our users create journals in a spreadsheet and upload in to Oracle vua web adi. They print a hard copy of the journal in Excel ADI template format as well as an output from Oracle of the journal to which their backup is attached. This is required for our audit/SOX requirements.
    Our users/audit review team are frustrated that the order of the lines in the adi xls file is not the same as in the actual oracle journal.
    Having looked at the journal template document I cannot find the journal line number as an option when creating the adi template. Is there any other way of ensuring that the line number in excel is the same as in Oracle (aside from re-ordering the excel lines into code combination order).
    We are running on Oracle Applications : 11.5.10.2
    Thanks

    As per (How To Add Line Number Column To Journal Entry Webadi Template [ID 1102405.1]) this feature is not available in R12 so this implies that it is not available in 11i.
    Thanks,
    Hussein

  • Line number in Sap Script

    hi,
    How to get line number in sap script form?
    Regards,
    Sal.

    hi ,
    there is no specific field for this you have to put a counter for that check these links
    Link for SAP Scripts (step by step procedure)
    http://smoschid.tripod.com/How_to_do_things_in_SAP/How_To_Create_A_SAPSCRIPT_Form/How_to_Create_SAPSCRIPT.html
    SAPScripts
    http://esnips.com/doc/1ff9f8e8-0a4c-42a7-8819-6e3ff9e7ab44/sapscripts.pdf
    http://esnips.com/doc/1e487f0c-8009-4ae1-9f9c-c07bd953dbfa/script-command.pdf
    http://esnips.com/doc/64d4eccb-e09b-48e1-9be9-e2818d73f074/faqss.pdf
    http://esnips.com/doc/cb7e39b4-3161-437f-bfc6-21e6a50e1b39/sscript.pdf
    http://esnips.com/doc/fced4d36-ba52-4df9-ab35-b3d194830bbf/symbols-in-scripts.pdf
    http://esnips.com/doc/b57e8989-ccf0-40d0-8992-8183be831030/sapscript-how-to-calculate-totals-and-subtotals.htm
    SAP SCRIPT FIELDS
    http://help.sap.com/saphelp_erp2005vp/helpdata/en/d1/8033ea454211d189710000e8322d00/content.htm
    scripts easy material
    http://www.allsaplinks.com/sap_script_made_easy.html
    regards,
    venkat.

  • Please hep to generate same line number to print in file

    Hi
    I am generating file by taking data from few tables. I need to generate file data from below structure data :
    i.e In table A for order O having product p1,p2,p3,p4, lines will be wrriten to file in following manner
    Line Number
    O -p1 - 1
    O -p2 - 2
    O -p3 - 3
    O -p4 - 4
    Now another line is from table which will have only two product entries for the order O in below manner
    i.e In table B for order O having product p2,p4. so for this lines should be match with above line number of order like this :
    Line Number
    O - p2 -2
    O - p4 -4
    First all data from table A will be writeen. Then only from TABLE B data should be wrriten to file.
    Can anybody please help how to match line number for both the tables?
    Thanks in advance

    Perhaps a clearer output would be if we had different descriptions on each of the tables A an B to output with each of the rows..
    SQL> ed
    Wrote file afiedt.buf
      1  with A as (select 1 as order_id, 'p1' as product_id, 'A - order 1 product 1' as descr from dual union all
      2             select 1, 'p2', 'A - order 1 product 2' from dual union all
      3             select 1, 'p3', 'A - order 1 product 3' from dual union all
      4             select 1, 'p4', 'A - order 1 product 4' from dual union all
      5             select 2, 'p1', 'A - order 2 product 1' from dual union all
      6             select 2, 'p2', 'A - order 2 product 2' from dual union all
      7             select 2, 'p3', 'A - order 2 product 3' from dual)
      8      ,B as (select 1 as order_id, 'p2' as product_id, 'B - order 1 product 2' as descr from dual union all
      9             select 1, 'p4', 'B - order 1 product 4' from dual union all
    10             select 2, 'p3', 'B - order 2 product 3' from dual)
    11  -- end of test data
    12  select order_id
    13        ,product_id
    14        ,row_number() over (partition by order_id order by product_id) as line_number
    15        ,descr
    16  from A
    17  union all
    18  select order_id, product_id, line_number, descr
    19  from (
    20    select b.order_id
    21          ,b.product_id
    22          ,row_number() over (partition by a.order_id order by a.product_id) as line_number
    23          ,b.descr
    24    from A left outer join B on (a.order_id = b.order_id and a.product_id = b.product_id)
    25    )
    26* where order_id is not null
    SQL> /
      ORDER_ID PR LINE_NUMBER DESCR
             1 p1           1 A - order 1 product 1
             1 p2           2 A - order 1 product 2
             1 p3           3 A - order 1 product 3
             1 p4           4 A - order 1 product 4
             2 p1           1 A - order 2 product 1
             2 p2           2 A - order 2 product 2
             2 p3           3 A - order 2 product 3
             1 p2           2 B - order 1 product 2
             1 p4           4 B - order 1 product 4
             2 p3           3 B - order 2 product 3
    10 rows selected.
    SQL>

  • How do I keep account of line and column number within JTextArea?

    How do I keep account of line and column number within JTextArea?
    Thanks.

    hi kamlesh,
    to get the number of columns in a JTextArea u have the function
    getColumns() which gives u the number of columns in the text area.
    To get the number of rows present u have the function
    getRowCount() which returns an int that specifies the number of rows in the text area.
    Hope this helps
    Cheers :)
    Nagaraj

Maybe you are looking for

  • How do I get Pages docs to iCloud?

    I've only have a Macbook Pro running Lion--no iphone or ipad.  I've made the movedfrom MobileMe to iCloud.  Mail, Contacts, and Calender have uploaded and are there, but the iWork icon sends me to a page that says, "To get started, launch Pages on yo

  • Material Replication problem from SAP ECC to CRM 2007

    I am trying to load material data from SAP to CRM.  Materials are in general coming across however i have a lot of errors visible in smw01 with the following entries: @0A@     BDoc validation error     PRODUCT_MAT     mBDoc Validate     06/04/2010   

  • WF start condition : PO type field in BUS2012

    Hi all ! I am creating a Workflow process whenever a PO ( Order type NB ) is changed. For this I am using the standard business object BUS2012 ( Purchase Order ) For my objective, I need to use the start Condition of PO type eq 'NB'. I go to Basic da

  • Windows Server 2008 Activation

    Hi All, As mentioned at Microsoft website Windows Server 2012 eligible to downgrade to Windows Server 2009. I had already installed Windows Server 2008, can I know how to activate it? I failed to activated it using my Windows Server 2012 license key.

  • Shared Dim in Load File

    I need to upload a couple of alternate hierarchies, but nowhere in the properties section can I see how to denote that the dimensions rolling up to the parent should be 'shared'. I.e., ~ is do not consolidate, but what's shared ?????