Results 1 to 14 of 14
  1. #1
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default keep track of LineNumbers in a JTextarea

    Hello I want to keep track of the LineNumbers of a JTextarea. I know that you can keep track of the LineNumbers in a file by using LineNumberReader.

    But I am writing a texteditor and sometimes you want to start a new File and before you safe it I want also be able to write out the LineNumbers.

    Does annyone knows if it is possible what I want?

    keffie91
    Never give up! ;)

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Of course it is possible,examine my editor ,which i wrote this year SourceForge.net: Sampad
    If you need the source code,tell me.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    Trap the append calls and count the newlines as text is added to the text area.
    Get the text area's text and count the number of newlines.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    i will tell you the idea how i implemented this and also how it is implemented in Eclipse (LineNumber track consists of JPanel and JLabels with numbers):
    1.create private class which implements DocumentListener and it extends Thread.
    Java Code:
    private class MyDocumentListener extends Thread  implements DocumentListener {
    		 public void changedUpdate(DocumentEvent e) {
    	        //Plain text components do not fire these events
    	          }
    		
    	    public void insertUpdate(DocumentEvent e) {
                   
                        Document doc = (Document)e.getDocument();
    2.then you should know every time when you type the rows size of the JTextArea,remember everything in this class(supposing that your JTextArea is tdefined as textArea):
    Java Code:
    int rows=textArea.getLineCount();
    3.removes evertyhing from your LineNumbers panel (supposing that LineNumber track is JPanel lineNumberPanel):
    Java Code:
    lineNumberPanel.removeAll();
    4.Add again labels on the line number count panel and repaint it:

    Java Code:
    for(int i=1;i<=rows;i++){
    	        	JLabel lineCount=new JLabel(""+i);
    	        	lineCount.setFont(textArea.getFont());
    	        	lineNumberPanel.add(lineCount);
    	        	
    	        }
                   lineNumberPanel.updateUI();
    }
    5.the same you can do when you remove the line:
    Java Code:
    public void removeUpdate(DocumentEvent e) {}
    I hope you understood.Welcome

  5. #5
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    Ok thanks, but I don't know exactly where to implemt the code where do I have to put all the pieces of the code, In the methods?. And do I have to put the class MydocumentListener in a new file? Because in eclipse it is not possible to create a private class.

    thanks keffie91
    Last edited by keffie91; 09-19-2008 at 02:11 PM.
    Never give up! ;)

  6. #6
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    don't forget to set the size of the line number panel and to adjust the layout to it.And for better performance the color:

    Java Code:
    lineNumberPanel.setPreferredSize(new Dimension(15, textArea.getHeight()));
    lineNumberPanel.setBackground(Color.gray);
    lineNumberPanel.setLayout(new BoxLayout(lineNumberPanel,BoxLayout.Y_AXIS));

  7. #7
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    Can you sent me the code of your texteditor, because I don't understand what lines of code i have to put where in my code.

    thanks keffie91
    Never give up! ;)

  8. #8
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    I hope you won't spend two weeks on examining my code ,it is 500kb weight.
    Give me your email please,remember man it is GPL license.

  9. #9
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    Can you maybe tell me where i have to put these lines:

    lineNumberPanel.removeAll();
    int rows=textArea.getLineCount();

    for(int i=1;i<=rows;i++){
    JLabel lineCount=new JLabel(""+i);
    lineCount.setFont(textArea.getFont());
    lineNumberPanel.add(lineCount);

    }
    lineNumberPanel.updateUI();
    }

    in the class MyDocumentListener or in the class of my editor?

    thanks keffie91
    Never give up! ;)

  10. #10
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    In the class editor create the private class MyDocumentListener which implements DocumentListener and extends Thread ,then add in method
    public void insertUpdate(DocumentEvent e) your code.

  11. #11
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    Ok that is clear. It is working a bit now. I write out the LineNumbers in a JLabel, but they are written out horizontal. And that is just not what I want. I want them vertical.

    I have thought this:
    JLabel lineCount= new JLabel( "\n" + i);

    \n is the standard character for a new line. But it doesn't work.
    Does anyone knows why this is not working?

    thanks keffie91
    Never give up! ;)

  12. #12
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    adjust the layout for the lineNumberPanel:
    Java Code:
    lineNumberPanel.setLayout(new BoxLayout(lineNumberPanel,BoxLayout.Y_AXIS));

  13. #13
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    I tried to think of a method to remove the LineNumbers. But it is not so easy as i thought. Can you give me some hints

    thanks keffie91
    Never give up! ;)

  14. #14
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Have you implemented the adding numbering the lines in the text area?
    Ok for removing you should again to create in the same class the method called removeUpdate(Document e) like this:

    Java Code:
     public void removeUpdate(DocumentEvent e) {
    	 
    	    	
    	    	int rows=textArea.getLineCount();
    	    	lineNumberPanel.removeAll();
    	        for(int i=1;i<=rows;i++){
    	        	JLabel lineCount=new JLabel(""+i);
    	        	lineCount.setFont(textArea.getFont());
    	        	lineNumberPanel.add(lineCount);
    	        	
    	        	
    	        }
    	        lineNumberPanel.updateUI();
            	
    	        
    	    }

Similar Threads

  1. JAVA Fast Track Course
    By fortius_computers in forum Reviews / Advertising
    Replies: 0
    Last Post: 08-30-2008, 09:55 AM
  2. How to track client logout time and orignal ipaddress (not gateway) in java
    By psandeep in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-13-2008, 12:32 AM
  3. How to manipulate JtextArea
    By bachtoutou in forum New To Java
    Replies: 8
    Last Post: 05-22-2008, 09:36 AM
  4. JTextArea
    By saytri in forum New To Java
    Replies: 0
    Last Post: 01-13-2008, 01:07 AM
  5. Track download
    By nilesh.malode in forum Advanced Java
    Replies: 1
    Last Post: 07-13-2007, 09:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •