The Artima Developer Community
Sponsored Link

Java Answers Forum
Line numbers for editor using JTextArea

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
cruise

Posts: 7
Nickname: cruise
Registered: Apr, 2002

Line numbers for editor using JTextArea Posted: Apr 4, 2002 4:26 AM
Reply to this message Reply
Advertisement
I am developing a small editor applet using JTextare with line numbers .
I created a panel with JTextArea for displaying line numbers.
For showing line numbers i addded document listener to the Text area
and appending the line number.(I am attaching the code below).
Its working fine when i the wrap mode is off, but when
i set the wrap mode on, i have problem in dispalying
the line numbers...
My main doubt is ..
How can we get the position of cursor in the text area
I want to know the line number where the cursor is located.

//editpanel is the textarea for Editing
//linepanel is the textarea for line numbers
//Adding document listener

editPanel.getDocument().addDocumentListener( new DocumentListener()
{
public void insertUpdate(DocumentEvent e)
{
int i=lineNum;
for(;i<editPanel.getLineCount();i++)
{
linePanel.append(String.valueOf(i+1)+"."+"\n");
lineNum++;
}
save.setEnabled(true); //enable the save button if content is changed
}

public void removeUpdate(DocumentEvent e)
{
save.setEnabled(true); //enable the save button if content is changed
linePanel.setText("");
for(int i=0;i<editPanel.getLineCount();i++)linePanel.append(String.valueOf(i+1)+"."+ "\n");
lineNum=editPanel.getLineCount();
}

public void changedUpdate(DocumentEvent e)
{
save.setEnabled(true);//enable the save button if content is changed
}

});

The above code works well if wrap mode is off, but when we switch the wrap mode on ..
this is not working.

Thanks in advance
Cruise

Topic: TreeMap problems! Previous Topic   Next Topic Topic: How to show 5 records per page using JSP?.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use