Total no. of lines in JTextPane document and Increment them on "ENTER" key

hello,
I wanted to know the total number of lines that a document has. I.e for example when I open a .java into my JTextPane, I wanted to know the total no. of lines the file has and I wanted to increment them when I press the enter key.
2. Even it is a plain document, i.e just typing in the JTextPane and when I press enter key the number of lines should get incrementing.
I am using this method , but un successful. Can any one help me?
public int getLines()
          Document doc = text.getDocument();
          Element map = doc.getDefaultRootElement();
          int n = map.getElementCount();
          Element lastLine = map.getElement(n-1);
          if ((lastLine.getEndOffset() - lastLine.getStartOffset()) > 1)
               return n;
          return n - 1;
where text = JTextPane
Any example source will be helpful.
Thanks

Your code looks correct. Here is the program I used to test it. You must be calling the getLines() method BEFORE the document is being updated.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class TestTextArea extends JFrame
     public TestTextArea()
          JPanel panel = new JPanel();
          setContentPane( panel );
          panel.setLayout( new BorderLayout() );
          final JTextPane textArea = new JTextPane();
          JScrollPane scrollPane = new JScrollPane( textArea );
          panel.add( scrollPane );
          JButton button = new JButton("Display Lines");
          panel.add( button, BorderLayout.SOUTH );
          button.addActionListener( new ActionListener()
               public void actionPerformed(ActionEvent e)
                    Document doc = textArea.getDocument();
                    Element map = doc.getDefaultRootElement();
                    int n = map.getElementCount();
                    Element line = map.getElement(n-1);
                    if (line.getEndOffset() - line.getStartOffset() == 1)
                         n--;
                    System.out.println( n );
                    textArea.requestFocus();
     public static void main(String[] args)
          TestTextArea frame = new TestTextArea();
          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
          frame.pack();
          frame.setVisible(true);
}

Similar Messages

  • Pages 5.2 - When I try to reorder different pages it just takes all the pages within the document and turns them into a stack! Why is this happening and how can I oder pages as the update is meant to let me?

    When I try to reorder different pages it just takes all the pages within the document and turns them into a stack! Why is this happening and how can I oder pages as the update is meant to let me?
    Any Help?I know the update has only been out a while but if anyone has an idea why it is doing this, please let me know!

    But you are telling me something I already know.
    Be precise.
    It is not Pages '09 that is broken.
    I am back in Mavericks and testing Pages 5.2.
    There are improvements, we shall see how useful, but in less than a minute I have already discovered bugs.
    Peter

  • HT5636 I was typing in a word for Mac document and I hit the apple key and the numeral 8 at the same time now all of my documents have these strange symbols please help

    I was typing in a word for Mac document and I hit the apple key and the numeral 8 at the same time now all of my documents have these strange symbols please help

    Simply type command 8 again. That toggles the "show paragraphs" function.

  • HT2470 how would i go about taking multiple files from documents and make them part of 1 master file

    how would i go about taking multiple files from documents and make them into 1 master file? I want to put all documents associated with this project together for easier access.

    From the finder window, click File, click new folder, you can drag all documents into that Folder making it the master folder. You can ajso drag folders into the Master folder, and have them as sub-folders if you wish to  do that. Either way they will all be in one master folder.

  • I had a trial version of Pages for 30 days and then purchased the actual software without removing the trial. Now when I try to edit documents and save them it wont because it tells me I am using a trial version.... How can I remove the trial version?

    I had a trial version of Pages for 30 days and then purchased the actual software without removing the trial. Now when I try to edit documents and save them it sometimes wont because it tells me I am using a trial version.... But sometimes it lets me!! How can I remove the trial version? I cannot tell which one I am using when I open it....

    Question asked and answered quite once a week.
    Go to my iDisk (address below)
    and download :
    For_iWork:iWork '09:uninstall iWork '09.zip
    Run it then reinstall the purchased software.
    Yvan KOENIG (VALLAURIS, France)  vendredi 2 janvier 2011 16:21:41
    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.2
    My iDisk is : <http://public.me.com/koenigyvan>
    Please :
    Search for questions similar to your own
    before submitting them to the community

  • I have scanned about 1600 pages of 100-year old documents and saved them as pdf files.  I would like to search them for keywords and phrases.  Can anyone recommend software that can be used as an index for these documents?

    I have scanned about 1600 pages of 100-year old documents and saved them as pdf files.  I would like to search them for keywords and phrases.  Can anyone recommend software that can be used as an index or search engine for these documents?

    If you have Adobe Acrobat (not Adobe Reader) the program can perform an OCR on the document you acquired.
    In the past I used ABBYY FineReader

  • How do I set up pages for mac and ipad to share a document and keep them synced.

    How do I set up pages for mac and ipad to share a document and keep them synced.

    You can upload them from Mac to iCloud and then download on the iPad. They only directly sync between iOS devices. The Mac version of Pages has more options that are not available on iOS. If you use a font that is not available on iOS it will change the font. Some layout options aren't available either.

  • I can't open personal accounts on the internet. After entering ID and Password then the enter key, it's as if the screen refreshes itself and wants me to start over. Help Please.

    I can't open personal accounts on the internet. After entering ID and Password then the enter key, it's as if the screen refreshes itself and wants me to start over. Help Please.

    Click here and follow the instructions.
    (72059)

  • Free application to view microsoft document and use iphone as USB key

    I have more than 10G space, I want to use iphone as USB key, I download a free app. USB key, but it can only view *.html in root directory, anyone know other free app to send my 250Mb project document to iphone,? I try total commander, but I don't know which sub folder of my iphone I should put my project document.
    also anyone know how to browse and edit word, excel, ppt in iphone, any free app?
    my ibooks can only view PDF.?
    anyone knows any free app like windows explorer in iphone, so we can can touch any microsoft office document and open it.?
    in iphone 4, what are the file type for video and movie, anyone know how to open *.avi in iphone?

    Let's address your questions one at a time.
    Using your iPhone as a USB drive.
    There are many apps for this and even some programs that can be installed on your PC/Mac. Apps on the actual phone I don't have any experience with, but a quick Google will give you a variety of choices. On my computer I like to use iPhone Explorer which works on both PC and Mac.
    You said you tried Total Commander, unfortunately I'm not sure what folder you would put it in, maybe in the root create a new subdirectory and place your project in there?
    iBooks!
    You can read PDF's or ePubs, PDFs can be opened into the iBooks app simply by downloading the PDF through Mobile Safari or Mail or even any other app. As for ePubs these unfortunately have to be managed through iTunes. Simply find your favourite book in ePub format, drag it onto the iTunes window and sync your phone. You should now see you new book in you iBooks library.
    I'm guessing you want to be able to explore all the files on your phone and open them. Unfortunately because of how Apple locked up the device that's not really possible (unless you break free ;)) however some apps allow you to create files and folders, but alas I have no experience with these. Though you could upload the documents to Dropbox and then use their app to view the documents.
    Movies need to be encoded as MP4 using the h.264 codec, I would suggest converting your videos with Handbrake on your PC or Mac and then syncing them through iTunes.
    Unfortunately the iPhone doesn't natively support .avi files, at a time there was the VLC app which works wonderfully unfortunately the developers pulled it. I suggest Googling around a bit and failing that you can always convert them.
    Hope that helps!

  • How can I take data from multiple pages documents and put them into a numbers table?

    I produce invoices in pages, with dates, invoice numbers, references and amounts due. I want to take all this data from multiple documents and transfeer it to a single numbers table. Is this possible and if so, how do I do it. I know I can do it the other way round with mail merge but I can't figure out how to do it this way round?
    Thanks,
    Keith

    The data is spread throughout a pages document in specific areas here's a copy of an invoice for you to have a look at.

  • How can I copy Word Document and save them in my iPad?

    Hi,  I just recieved my iPad 3 today. I've never used the Apple format before.  All of my recipes are in Microsoft Word all neatly organized, there are many... I mean a lifetime of them and I'm old!  I bought the iPad to use it in the kitchen and for the life of me I can't seem to figure out how to transfer my Word files to my iPad and put them in a document folder.  Can anyone help?  I sure would appreiciate it. 

    There are no document folders in the iPad. Files and documents are saved within the app that is used to create, edit and view the corresponding files. You need an app that can read and edit Word files and you will be able to save the files within that app.
    You can email files, use DropBox or another cloud service, use iOS file sharing or use iCloud - if you use one of the Apple iWorks apps - in this case it would be Pages.
    There are MS Office compatible apps available in the app store like Quick Office Pro and Documents to Go. Take a look at those and see what you think.
    This is a link to Apple's word processing app - Pages

  • Why does my new garageband 10.0.03 not have the legacy with sound effects or radio sound preset or applause preset???? I need these to be able to chop samples into the keyboard keys by dragging and dropping them into the keys.

    I am just trying to chop samples and drop them into a track with legacy-sound effects- radio sounds, so that I can play with them and chop them onto keys. I see a bunch of videos on youtube showing you how to do it, but I can't find those kits on the newest garageband.

    All these effects are part of the loops in the iLife Sound effects loop package, that was a part of the application bundle iLIfe '11 (iPhoto. iMovie, GarageBand '11).  These loops are neither installed by GarageBand 10.0 or GarageBand '11, nor MacOs X, you need an iLife '11 installer disk to get these loops.
    And GarageBand 10 also does not have the Sound Effects instruments any longer, that let you drag samples onto keys. It is one of the sorely missed features, that have been discontinued in the new version GarageBand 10. even if you install the previous version, GarageBAnd '11, to add the Sound effects instrument to GarageBand 10. it will not work properly, because GarageBand 10 will not save the custom instrument with the samples. You will have to complete your song in one session, without quitting GarageBand.
    You may want to send feedback to the Apple Developers about these missing features with a feature request:   Apple - GarageBand Mac- Feedback

  • Loading documents and attaching them to business objects

    Hi,
    I am trying to load documents (Word and PDF files) into a content management server linked to a SAP CRM system. The documents need to be attached to business objects such as interaction records (BUS2000126) and service tickets (BUS2000116).
    I have experimented with FMs <b>BDS_BUSINESSDOCUMENT_CREATEF</b> and <b>ARCHIV_CONNECTION_INSERT</b> but haven't yet found the right combination of values. The best I have done is to create a link which is visible in the GUI as an icon, but when I double click on it, I get an error message saying that the document does not exist (error 404).
    The import parameters I am using in BDS_BUSINESSDOCUMENT_CREATEF are:
    LOGICAL_SYSTEM = [blank]
    CLASSNAME = ZCSIP_LTR (a value from table TOADV)
    CLASSTYPE = BO
    CLIENT = [default]
    OBJECT_KEY = [blank]
    FILES
      DOC_COUNT = 00000001
      COMP_COUNT = 00000001
      COMP_ID = [blank]
      DIRECTORY = U:\DOCUMENTS\TEST DATA\
      FILENAME = FA.PDF
      MIMETYPE = application/pdf
    SIGNATURE
      DOC_COUNT = 00000001
      PROP_NAME = BDS_CONTREP
      PROP_VALUE = ZT (my content repository name)
    The FM returns a GUID as the OBJECT_KEY ...
    Then I am using ARCHIV_CONNECTION_INSERT as follows:
    ARCHIV_ID = ZT
    ARC_DOC_ID = [GUID returned by BDS_BUSINESSDOCUMENT_CREATEF]
    AR_OBJECT = ZCSIP_LTR (a value from table TOADV - as above)
    MANDANT = [default client as above]
    OBJECT_ID = [GUID of the object I want to attach the document to]
    SAP_OBJECT = [Business object identifier] e.g. BUS2000126
    DOC_TYPE = PDF
    Can what I need to do be achieved through just using BDS_BUSINESSDOCUMENT_CREATEF? What import parameters should I use? What values should they contain?
    Many thanks in advance,
    Peter

    Hi,
        BDS classname BUS2032 and classtype BO
    but function module 'BDS_BUSINESSDOCUMENT_CREATEF' and cl_bds_document_set class method
    here is the code..
    DATA: o_document_set TYPE REF TO cl_bds_document_set,
    wa_signature LIKE bapisignat,
    i_signature LIKE bapisignat OCCURS 1,
    wa_files LIKE bapifiles,
    i_files LIKE bapifiles OCCURS 1,
    i_object_key LIKE bapibds01-objkey.
    CREATE OBJECT o_document_set.
    MOVE: '1' TO wa_signature-doc_count,
    '1' TO wa_files-doc_count,
    'c:\temp' TO wa_files-directory,
    '1.jpg' TO wa_files-filename.
    APPEND wa_signature TO i_signature.
    APPEND wa_files TO i_files.
    i_object_key = '0000261877'.
    BREAK-POINT.
    CALL METHOD o_document_set->create_with_files
    EXPORTING
    classname = 'BUS2032'
    classtype = 'BO'
    CHANGING
    object_key = i_object_key
    files = i_files
    signature = i_signature
    DATA : files LIkE BAPIFILES OCCURS 1 WITH header line.
    files-doc_count = '1'.
    files-directory = 'c:\temp\'.
    files-filename = '1.jpg'.
    APPEND files.
    CALL FUNCTION 'BDS_BUSINESSDOCUMENT_CREATEF'
    EXPORTING
    LOGICAL_SYSTEM =
    classname = 'BUS2032'
    classtype = 'BO'
    CLIENT = SY-MANDT
    OBJECT_KEY = '0000261877'
    IMPORTING
    OBJECT_KEY =
    tables
    files = files
    SIGNATURE =
    EXCEPTIONS
    NOTHING_FOUND = 1
    PARAMETER_ERROR = 2
    NOT_ALLOWED = 3
    ERROR_KPRO = 4
    INTERNAL_ERROR = 5
    NOT_AUTHORIZED = 6
    OTHERS = 7
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF
    And check the OSS Note 317250.
    <b>Reward points</b>
    Regards

  • How do I add new pages to a document and move them around?

    Before I used to be able to add new pages at any point in the document.
    I could also  restructure the document, changing the page orede as I pleased.
    I can't figure out how to do that now...
    Am I missing page Layout and only using word processing or is this just the new Pages program if so FIX IT APPLE I dont like it!

    Nonsense, everyone at Apple says you love it!
    Peter

  • Accessing two PDf documents and merging them

    Hi,
    I have and application where on click of a button, 2 pdfs are read from the server and merged into one pdf document. My pdf documents are placed in a folder in irj folder on server. For this i referred to the blog:
    /people/nagamohan.devisetty/blog/2008/05/05/merging-pdf-files-using-java-apis
    As per this blog, i created a java file.But I wrote the code written in the main method of this java file in the OnAction of the web dynpro view. So my java file just contains concat () for merging the pdfs. And from this view itself i created the object of the java file and called the concat (). But when i execute this application i get the error message stating "The system cannot find the path specified".
    Can anyone please help in this regard.
    Thanks & regards,
    Anupreet

    Hi,
    Was your previous application already web dynpro? Or did you include your concat fonctuinnality to a web dynpro application?
    From WD, if you want to write files, they are written by default in the server folder \usr\sap\J2E\JC00\j2ee\cluster\server0\persistent\developmentserver
    Check if you have rights to write out of this folder, in the portal.
    Yann

Maybe you are looking for

  • Variance Post against in internal orders...

    Hi Experts kindly suggest me on the following requirement 1.Firstly  informing Variance GL account had been created as a cost element 2.When We making the MIRO against CAPEX Purchase Orders if any variances will come system will post these variances

  • Various installation errors

    These don't appear to be serious, but several programs (notably photoshop, illustrator) installed with similar errors. 1) are these a big deal/something to worry about? 2) what can I do to fix these types of errors? a full listing of the errors from

  • Userexit while/before save adrc-data

    Hi there! I need a common userexit to save additional data each time anywhere in the system adrc-data is saved. This can be a changed or new adress data if there's a new customer as well as there's a new built in equipment for example. Is there such

  • Photoshop 3D crash

    When I try to do ANYTHING 3D in Photoshop it crashes it. It used to work fine, then I got the most up to date drivers for my video card, and 3D in Photoshop doesn't work anymore.. An error comes up if I click on anything 3D related and says "Photosho

  • How to extend the size of a layer?

    Hi all, Hopefully this is an easy one, but I think I'm just thinking about it wrong.  I'm animating the movement of one part of a photo.  I duped the photo layer, masked out the subject I want on the copy layer and have animated its position over tim