Jump to content

position of cursor in JEditorPane

- - - - -

  • Please log in to reply
12 replies to this topic

#1
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I need to display the position of the cursor of a JEditorPane in a JTextArea.
How can I do it? Also, when I click on the JTextArea it starts displaying crazy stuff(in the screenshot)!
What gives?

Posted Image



EDIT: The crazy stuff happened because the A value of the background color was 0.
So the remaining question is: how can I get the row and column of the cursor?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
add a MouseMotionListener to the JEditorPane:
JEditorPane pane = new JEditorPane();

pane.addMouseMotionListener(new MouseMotionListener(){

  public void mouseMoved(MouseEvent e) {

        int x = e.getX();

        int y = e.getY();

       textArea.setText(x + ", " + y);

    }


    public void mouseDragged(MouseEvent e) {

       //stays empty

    }

});


#3
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I'm sorry maybe I wasn't clear enough. I need something to tell me the row and column. Here a screen of what I mean: http://img695.images...24/74000281.jpg

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Ow, THAT cursor :D

Then you need a keylistener(typing/arrow keys) + mouselistener(mouseclick to move cursor)
Both listeners should use the
editorpane.getCaretPosition();
Which gives you a ... number. The number of the position in the text.
So editorpane.getText(); gives you the whole text in 1 string, the caretposition is the position in that string...
I suppose you'll need to count the \n and from the last one count the amount of positions to the caretposition to get column and row :/

#5
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Ok that's what I thought... I was hoping for a less heavy solution from a computational point of view

#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I tried but it's impossible. It always puts a carriage return before the newline (ascii codes 13 and 10), but the caret position is wrong! It's a total mess.
Just to give you an idea I did this:

	


//row and column are ints declared outside this method

public void setColAndRow(int currentPos) {

	char[]array = editorPanel.getText().toCharArray();

	int lastNewline=-1;

	row=1;

		

	for(int i=0;i<currentPos;i++){

	    if(array[i]=='\n'){

		row++;

		lastNewline=i;

	    }

	}

		

	column=currentPos-lastNewline;

}

But it behaves very weirdly.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Okay, the carriage return messed stuff a bit up... (and i discovered the CaretListener btw )
This seems to work... i've tested it for about 30 seconds so it's GOT to be fail-proof :D

public class Test extends JFrame {

    private JLabel label;


    public Test() {

        super();

        final JEditorPane ePane = new JEditorPane();

        ePane.addCaretListener(new CaretListener(){

            public void caretUpdate(CaretEvent e) {

                int pos = e.getDot();

                int row = 0, column=0;

                String text = ePane.getText().replaceAll("\r", "");

                if(pos+1<=text.length()){

                    text = text.substring(0, pos+1);

                    column--;

                } 


                while (text.contains("\n")) {

                    row++;

                    int start = text.indexOf("\n");

                    if (text.length() - start >= 1) {

                        text = text.substring(start+1, text.length());

                    }

                }


                column += text.length();

                label.setText(row + ", " + column + "  (" + pos + ")");

            }

        });

       

        label = new JLabel();

        add(ePane);

        add(label, BorderLayout.SOUTH);

        setSize(400,400);

        setVisible(true);

    }


    public static void main(String[] args) {

        Test test = new Test();

    }

}



#8
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Well, it doesn't (try writing a few lines and position the cursor at the end of a line that is not the last one), but this seems to work:

public void caretUpdate(CaretEvent e) {
				int pos = e.getDot();
		        int row = 1, column=0;
				int lastNewline=-1;
				String text = editorPanel.getText().replaceAll("\r", "");
				
				for(int i=0;i<pos;i++){
					if(text.charAt(i)==10){
						row++;
						lastNewline=i;
					}
				}
				
				column=pos-lastNewline;
				
				rowCol.setText("Col: " + column + "\nRow: " + row);
			}

Great idea using replaceAll :D

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

(try writing a few lines and position the cursor at the end of a line that is not the last one)
Noone ever does that :-P

na /jk. I'm glad you've figured it out :)

#10
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Thanks to you! ;)
I don't understand why Java has to make simple things so complicated though.

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
And i just noticed that when you resize the frame (or type a looooong sentence)so your text starts on a new line the rowcount and columncount is wrong, yet again, because there is no \n to count then...

#12
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Well, of course, but that't the behaviour I was looking for :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users