• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Counting Lines

 
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea a JTextArea. Sounds easy.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
textInput.getLineCount() returns the line number I am one, just have to figure out the column number. probably caretPosition - lineCount = columns.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic