Column number in JTextArea?

Hi
How do I get column number of current cursor
in JTextArea? I need to display this info
in my editor.
This is complicated by the fact that tab size
is not fixed, typing a combination of space
and tabs randomly can result in tabs
having tab size of 1 amid normal tabs.
Hence counting the number of chars in the
current line up to cursor position, call it C,
then C minus number of tabs, T and plus
T * tabSize won't work because tabSize is
sometimes 1.
Thanks
miawomi

Hello
Just figure out how to do this. In case anyone
get stuck with the same problem, here is my
soln:
JTextArea textArea = ...
int caretPos = textArea.getCaretPosition();
int lineCount = textArea.getLineCount();
int tabSize = textArea.getTabSize();
int currentLine = 1;
String str = "";
int c = 0;
try {
currentLine = textArea.getLineOfOffset( caretPos );
int startOffset =
textArea.getLineStartOffset (currentLine );
str = textArea.getText( startOffset,
caretPos-startOffset );
FontMetrics fm = textArea.getFontMetrics( textArea.getFont() );
                    StringBuffer buffer = new StringBuffer();
for ( int i = 0; i < tabSize; i++ )
buffer.append(" ");
int tabWidth = fm.stringWidth( buffer.toString() );
for ( int i = 0; i < str.length(); i++ ) {          char C = str.charAt( i );
                         if ( fm.charWidth( C ) == tabWidth )
                              c = c + tabSize;
                         else
                              c++;
               } catch ( BadLocationException ex ) {
                    ex.printStackTrace();
If anyone has a much less cpu intensive solution,
please let me know.
Thanks in advance
miaw

Similar Messages

  • 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

  • Getting the row and column number from the caretposition

    Howdy peeps
    Does anyone know how to find out the Line number and column in a JTextPane by using the caret position
    (you know like in text editors where it says 100:4 [line number:character position on row])
    is there a simple way of doing this in a JTextPane?

    im getting a bit confuzzled with this :(
    can someone help me out with a bit more code orientated help? :$
    i have a jTextArea and a JTextField, when i change the caret in the text area i want the JTextField to show the line number and charater position on the row (e.g. 100:4)
    so i assume i need to methods
    e.g.
    getPaneRow
    myint=myPane.getCaretPosition()
    do some wonderful magic code to get the line number
    return line number
    getPaneColumn
    myint=myPane.getCaretPosition()
    do some wonderful magic code to get the column number
    return column number
    is there a magician out there that can give me the magic powder? :)

  • Goto row and column in a JTextArea

    Does anybody here know the correct procedure to get to a specific row and column in a JTextArea? I want to be able to do something like this:
    replace(char c, int row, int column) - where the original character at row,column will be replaced with c.
    I know that in C++, there is a windows console library that lets u do this, but there isn't one in java so I'm trying to get around it using a JTextArea.
    Thank you.
    Any Help/Comments/Ideas will be greatly appreciated.
    -Patrick

    JTextArea has a replaceRange method.
    John

  • Displaying the Row and Column number in the report

    I am trying to show row and column number in the report (not just web preview). I am using Hyperion Reports Version 7.2.5.168

    use a formula column/row. use RANK function in that. (e.g. Rank([A], asc) will sort the rows based on column A values in ascending order)
    you can use this rank in your heading.
    But frankly this is not so easy. You have to do it in a very intelligent way, so that rank gives you column number/row number any how.
    Have a try and let see if you find a appropriate solution.
    Regards,
    Rahul

  • Summary Links web part not showing correct column number

    Here is my scenario. I have a simple two server dev farm that is using a snapshot of production data. I have migrated the 2010 content db over and everything is running fine. One quirk I have noticed is with the summary links web part. If I add the webpart
    to a page, in the configuration settings for the webpart you can create named groups and the next setting is how many columns you want the links to appear in. When I select lets say "4" columns, and I create four groups. When I save and check in
    the web part only shows "3" columns instead of 4. 
    For Example. 
    I create groups: Group 1
                              Group 2
                              Group3 
                              Group 4
    In the webpart configuration I select "display number of columns" = 4
    When I save that setting and look at the web part the groups will appear as:
     Group 1             Group 2                    Group 3
    Group 4
    when in reality if the column setting is = 4, I would expect: 
    Group 1      Group 2      Group 3      Group 4
    So it appears it is behaving as if whatever column number you select, what is displayed is N-1. 
    Has anyone else had this happen in SharePoint 2013? The same webpart doesn't behave this way in the 2010 production environment where dev gets its content. Any help would be great. 
    Thanks,

    Hi againeyuga,
    Yes, I have tested in my SharePoint Server 2013 with December 2013 CU  and it  works fine. For your environment, I recommend  you install the SP1 update.
    Best Regards,
    Eric
    Eric Tao
    TechNet Community Support

  • Why order by clause maintains column number values instead of column names

    why order by clause maintains column number values instead of column names ?

    we can use oder by 1,2 as column number
    UGNo one said that it can't be used. What's your point?
    To OP: It can be written with the column's 'select list positional number' just because for the support of laziness. :)
    There's no difference between 'ORDER BY name, address' and 'ORDER BY 1,2'.

  • Get the raw/column number of a left-mouse clicked table cell

    is this possible to get the row/column number of
    a cell where a mouse is clicked? Accutally, i want to implement a
    dynamic one column list which the user can "Add" or "Delete" some entries. The
    "Add" function is all right. The "Delete" function
    makes me upset because I cannot catch the cell's location where the
    mouse is choosing. I tries to use "Active Cell", it seems does not
    work. Do you have any clue? is this possible to implement the dynamic list using the "listbox"? thanks

    I would use the EditPos property value.  In my testing, it always returned the row and column of the cell I had left-clicked on.

  • Purchase order row basecard column number

    hi
    any one help me what is purchase order row basecard column number.
    Thanks & Best Regards
    B.Lakshmi narayanan

    Hi,
    Could you clarify your reguest?
    Thanks,
    J.

  • (wls61sp4)java.sql.SQLException: Invalid column number [jDriver for MSSQL]

    Hi,
    Could any one help me?
    We use weblogic 6.1 SP4 and SQL2000 SP3, and we weblogic's JDBC driver(mssqlserver4v65).
    It seems the JDBC connections is not stable and sometimes throw the exceptions:
    ==========================================
    java.sql.SQLException: Invalid column number (2). This table has 1 columns (a valid index is 1 trough 1).
    java.sql.SQLException: Invalid column number (2). This table has 1 columns (a valid index is 1 trough 1).
         at weblogic.jdbc.mssqlserver4.TdsResultSet.getEntry(TdsResultSet.java:193)
    ===========================
    Thank you.
    --Ted

    Ted wrote:
    Hi,
    Could any one help me?
    We use weblogic 6.1 SP4 and SQL2000 SP3, and we weblogic's JDBC driver(mssqlserver4v65).
    It seems the JDBC connections is not stable and sometimes throw the exceptions:
    ==========================================
    java.sql.SQLException: Invalid column number (2). This table has 1 columns (a valid index is 1 trough 1).
    java.sql.SQLException: Invalid column number (2). This table has 1 columns (a valid index is 1 trough 1).
    at weblogic.jdbc.mssqlserver4.TdsResultSet.getEntry(TdsResultSet.java:193)
    ===========================
    Thank you.
    --TedCan you reliably reproduce this? Are you sure the query being sent returns
    more than one column? You should be using the mssqlserver4v70, not v65, for
    sqlserver2000.
    Joe

  • Is there any option in JTable to add the column number

    Hi,
    I am using JTable to show some data read from database.
    I want to add the column number autometically.(Without by talking a variable and incrementing that by 1.After that add that to the first column.)
    I want just like a auto increment row number.
    Is there any option in JTable?
    Thanks

                        for(int i = 0; i < table.getRowCount(); i++){
                            table.setValueAt(i, i, 0);
                        }

  • A row delimiter should be seen after column number

    A column delimiter was seen after column number <70> for row number <533394> in file
    The total number of columns defined is <70>, so
                                                          a row delimiter should be seen after column number <70>. Please check the file for bad data, or redefine the input schema for
                                                          the file by editing the file format in the UI.
    So i checked the flat file i was trying to retrieve the data for the row number and checked the last column and did not find anything unsual in that row that could have caused this error.
    There are 70 columns and it proposes there is a problem in the last column. I checked but there were no carriage return
    Please help me on this.

    It appears to me the problem with the particular row(s).
    I suggest open the file in notepad if not very big and look at the record number 533394.
    you can also use u2018Overflow fileu2019 option in the error handling section on the target table under the option tab to find out the bad record(s).
    Regards,
    Vibhor

  • The phone column number is unknown

    Why I iPhone mobile phone set inside the phone column number is unknown?My number is telecommunications, also restore settings, for other telecom card, but no effect。Why?

    I have the same problem too with my iPhone 5. I tried putting my sim card in another iPhone 4 to change the number, but that didnt work either. My iMessage and Facetime can only be seen with my email and not my number.

  • Column number of caret position

    How can I get column number of caret position. Of course I can use
      int dot = editorPane.getCaret().getDot();
      Element root = editorPane.getDefaultRootElement
      int lineIndex = root.getElementIndex(dot);
      Element lineElement = root.getElement(lineIndex);
      int lineStartOffset = lineElement.getStartOffset();
      int column = dot - lineStartOffset + 1;
    to find the column in a unidirectional text! But what I should do for bidirectional text that has Hebrew, Arabic, or another RTL language? Also with a monospaced font the code
      int dot = editorPane.getCaret().getDot();
      double x = editorPane.modelToView(dot).getX();
      int column = (int) x / someFontMetrics.stringWidth("0") + 1; This won't help anymore. What should I do? The code
      editorPane.getUI().modelToView(editorPane, dot, dotBias) won't work too. First because the dotBias cann't be accessed in DefaultCaret. Second in some cases it probably won't solve the problem.

    Thanks Stats! It won't work!
    Hmmm.... Actually many unicode characters have no width, such as \u064E (Arabic Fatha). Some of them are only for visual ordering purpose, such as \u202A (Left to Right Embedding).
    Paying more attention to English letters, we'll notice ligatures used in some fonts. For instance occurrence of the letters fi (f and i) will produce the one letter &#64257; (\uFB01).
    We saw the algorithm you suggest won't work even for English text! Two ways we have. (In fact these are two mthods I can suggest!)
    1. Using the final output after affecting ligatures and bidirectional algorithm, finding the position of the desired character!
    2. Using xy position of the caret and derive the column number for a fixed font.
    Actually I want to use the second way though the first one is easier and more efficient. That's it! All I want is just the xy position of the caret!
    But ANY IDEA?!

  • XML column letter as opposed to column number

    Hi,
    I upload an XML file to and xstring and used a series of IF_IXML* interfaces to capture it's contents.  Thru these, I'm able to get the column count.  But, I can't seem to find a way to get the corresponding letter of the columns, i.e., A B C, etc. Not the cell name as in A1, but just the column letter.
    Is there a way to get the column letter?  This way, the user may enter the letter in an input box on the screen. Using that letter, I'll know which column to get the data from instead of  having the user count the column number and enter it in the input box.
    Thanks,
    Huntr

    Any takers ?

Maybe you are looking for

  • Problems using iPhoto on an iBook G3

    I have an iBook G3 with OSX 10.4.2. Frequently (two times!) it start erasing all the icons of my library of photographs, then, if I recharge the pictures in the library, I can see the icons again but near of it there is an "empty" icon with the same

  • How do I know what to put in when it says server when setting up?

    I have tried the IPv4 address, and many others but I can't find one that works. I am presuming that the "Name" is your computers Host name and the password is the Host password. Can someone please help? I'm really lost, it would be simple apart from

  • Errors in my console log attributable to IPv6

    I was getting these errors every minute or so. 05/24/10 7:10:11 PM configd[14] network configuration changed. 05/24/10 7:11:21 PM configd[14] network configuration changed. 05/24/10 7:11:22 PM configd[14] network configuration changed. 05/24/10 7:12:

  • Implementing WITH clause in ODI

    Hi, I want to convert certain 'WITH' clauses and 'Inline' views of a SQL query in ODI interfaces.Is there any way I can do this? Regards, RAshmik

  • Can we call System.gc() in  finalize()

    as we know that finalize method call implicitly call when Object is destroy. But in our code we use System.gc() explicitly call in finalize method. whether it is right or wrong? eg. protected void finalize() throws Throwable{ try { System.gc(); } cat