File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Counting Lines Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Counting Lines" Watch "Counting Lines" New topic
Author

Counting Lines

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I have this notepad program and I'm wanting it to display the line number and column number that the cursor is on. Any ideas?


Visit my blog! http://jameshambrick.com
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
What kind of component are you using to display the text? If it's a JTextArea you can use getCaretPosition to determine the position of the caret. then you'd use getColumns to determine the in which row and column the caret is. There are also getSelectionStart and getSelectionEnd methods if that is applicable in your case.


Ping & DNS - updated with new look and Ping home screen widget
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Yea a JTextArea. Sounds easy.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Sounded easy until I tried it out. I added a key listener and this it the code I have.
No matter if I press the enter key or the space bar is adds one to the counter. How do I sort this into lines and columns?

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

textInput.getLineCount() returns the line number I am one, just have to figure out the column number. probably caretPosition - lineCount = columns.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
Neat problem My naive approach hadn't considered the possibility of newlines in the text. But there's a method called JTextArea.getLineOfOffset which sounds as if it might give you the line number if you pass it what I assume is the caret position. In conjunction with getLineStartOffset you should be able to work out the column as well.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

While im looking at that. I am going to be converting that number to a string and putting it inside a textarea. While defining the texarea called inforArea3 we can specify a string for it to display, but I need to also have it a certain width.

I have already engineered a solution!


[ November 18, 2007: Message edited by: James Hambrick ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I think that if I can determine when the user presses the enter key then I can get the column number


if that does not work either!!! it resets it back to 0 when I press enter, but then when I press the right arrow key it adds1 to whatever number it was before it was 0 then displays that.
[ November 18, 2007: Message edited by: James Hambrick ]

[ November 18, 2007: Message edited by: James Hambrick ]
[ November 18, 2007: Message edited by: James Hambrick ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

getLineOfOffset requires to give it an offset! I don't know what an offset is or how to get one.

[ November 18, 2007: Message edited by: James Hambrick ]
[ November 18, 2007: Message edited by: James Hambrick ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
I would have assumed that the offset is the same as the caret position, but I've never used these methods, so this is just a wild guess.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[James Hambrick;]   While im looking at that. I am going to be converting that number to a string and putting it inside a textarea

String offset = Integer.toString(infoArea3.getCaretPosition());

Where you have:

System.out.println(caretPosition);

You should be able to come up with some kind of status bar for the window and just make the Integer.toString call inside a set text call for that status bar.

I can help you find it if you get stuck.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Nicholas Jordan HUH???

I have infoArea3 to display the column number and infoArea2 to display the line number. I am using System.out.println to just see if it will work first. this way I can keep up with the previous values before they changed. I am keeping track of cursor position(lines and columns) on textInput.

it complains when I use carePosition, it throw BadLocationException

[ November 18, 2007: Message edited by: James Hambrick ]
[ November 18, 2007: Message edited by: James Hambrick ]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I was coding in a hurry, there should be some method by which caret column, caret line number or cursor or whatever it is can be called, if it is throwing a bad location exception, it may be that cursing carrot was never corralled in the viewable area of the text area or whichever variable it is supposed to be.

That would likely be corrected by calling set carrot position() or someting, it will all be documented. Where are you looking for your documentation so that I can synchronize with you ?

I understood the System.out syntax, I was trying to jump up on an as yet unforseen hang, I suggest you do your Integer.toString() to the text area for now, during prototyping: It hangs less and runs smoother.

Replace infoArea3 with textInput in my post. Can we see some more code ? be sure to use the code tags if you post it. Are you using JText or awt ?

[ November 18, 2007: Message edited by: Nicholas Jordan ]
[ November 18, 2007: Message edited by: Nicholas Jordan ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

using swing. texInput is a JTextArea and so is infoArea2. None of these work right now. I may have to check to see which key was pressed and then alter a lineCount/columnCount accordingly. Also would have to take in consideration the word wrap too. I just figure there's a simpler way.

Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[James Hambrick:]   None of these work right now.
Text area uses peering, which I have no idea how that works, but I bet the text area is working and is just not being painted on the screeen.

Also do: isEditable() / synchronized void setEditable(boolean b) to see if it influences how the area behaves during test runs. I would blindly put public synchronized void setCaretPosition(int position) in the code somewhere early for now until a greater effectiveness is achieved. That method is in the immediate superclass of TextArea and I prefer to make the call as super.setCaretPosition((int) 0x00000000); which is gonna get us a lotta moanin but you may make the call in the conventional setCaretPosition( 0 ); because the place to look for it is understood by the context of where it is written.

Your posted code looks logically effective to me, I suggest for the moment going to Swing / AWT / SWT / JFace and posting that your code is ( probably ) not drawing the window, there are some really knotty problems that have to do with deeply buried issues and I wouldn't sleep tonight thinking we were going to have to work those in beginnner.

There are so many issues involved that you should just go there and do whatever contemporary practice is. When you get back, consider posting in intermediate: A question like this can get into hundreds of lines of code and it *could* confuse some beginners.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

the textArea is drawn and works fine, just the line count and column count dies not work. I will post it in the other area you suggested too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Counting Lines
 
Similar Threads
How to calculate space complexity for JPA
Float precision problem?
WA #1.....word association
Struts doubt
maximum concurrent session in apache tomcat