Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-18-2008, 07:23 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
keffie91 is on a distinguished road
keep track of LineNumbers in a JTextarea
Hello I want to keep track of the LineNumbers of a JTextarea. I know that you can keep track of the LineNumbers in a file by using LineNumberReader.

But I am writing a texteditor and sometimes you want to start a new File and before you safe it I want also be able to write out the LineNumbers.

Does annyone knows if it is possible what I want?

keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-18-2008, 09:17 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 356
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Of course it is possible,examine my editor ,which i wrote this year SourceForge.net: Sampad
If you need the source code,tell me.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-18-2008, 11:06 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Trap the append calls and count the newlines as text is added to the text area.
Get the text area's text and count the number of newlines.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-19-2008, 01:02 AM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 356
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
i will tell you the idea how i implemented this and also how it is implemented in Eclipse (LineNumber track consists of JPanel and JLabels with numbers):
1.create private class which implements DocumentListener and it extends Thread.
Code:
private class MyDocumentListener extends Thread implements DocumentListener { public void changedUpdate(DocumentEvent e) { //Plain text components do not fire these events } public void insertUpdate(DocumentEvent e) { Document doc = (Document)e.getDocument();
2.then you should know every time when you type the rows size of the JTextArea,remember everything in this class(supposing that your JTextArea is tdefined as textArea):
Code:
int rows=textArea.getLineCount();
3.removes evertyhing from your LineNumbers panel (supposing that LineNumber track is JPanel lineNumberPanel):
Code:
lineNumberPanel.removeAll();
4.Add again labels on the line number count panel and repaint it:

Code:
for(int i=1;i<=rows;i++){ JLabel lineCount=new JLabel(""+i); lineCount.setFont(textArea.getFont()); lineNumberPanel.add(lineCount); } lineNumberPanel.updateUI(); }
5.the same you can do when you remove the line:
Code:
public void removeUpdate(DocumentEvent e) {}
I hope you understood.Welcome
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-19-2008, 09:17 AM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
keffie91 is on a distinguished road
Ok thanks, but I don't know exactly where to implemt the code where do I have to put all the pieces of the code, In the methods?. And do I have to put the class MydocumentListener in a new file? Because in eclipse it is not possible to create a private class.

thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by keffie91 : 09-19-2008 at 04:11 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-19-2008, 06:02 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 356
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
don't forget to set the size of the line number panel and to adjust the layout to it.And for better performance the color:

Code:
lineNumberPanel.setPreferredSize(new Dimension(15, textArea.getHeight())); lineNumberPanel.setBackground(Color.gray); lineNumberPanel.setLayout(new BoxLayout(lineNumberPanel,BoxLayout.Y_AXIS));
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-19-2008, 06:31 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
keffie91 is on a distinguished road
Can you sent me the code of your texteditor, because I don't understand what lines of code i have to put where in my code.

thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-19-2008, 06:45 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 356
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
I hope you won't spend two weeks on examining my code ,it is 500kb weight.
Give me your email please,remember man it is GPL license.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-19-2008, 06:49 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
keffie91 is on a distinguished road
Can you maybe tell me where i have to put these lines:

lineNumberPanel.removeAll();
int rows=textArea.getLineCount();

for(int i=1;i<=rows;i++){
JLabel lineCount=new JLabel(""+i);
lineCount.setFont(textArea.getFont());
lineNumberPanel.add(lineCount);

}
lineNumberPanel.updateUI();
}

in the class MyDocumentListener or in the class of my editor?

thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-19-2008, 06:57 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 356
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
In the class editor create the private class MyDocumentListener which implements DocumentListener and extends Thread ,then add in method
public void insertUpdate(DocumentEvent e) your code.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 09-19-2008, 08:23 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
keffie91 is on a distinguished road
Ok that is clear. It is working a bit now. I write out the LineNumbers in a JLabel, but they are written out horizontal. And that is just not what I want. I want them vertical.

I have thought this:
JLabel lineCount= new JLabel( "\n" + i);

\n is the standard character for a new line. But it doesn't work.
Does anyone knows why this is not working?

thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 09-19-2008, 08:28 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 356
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
adjust the layout for the lineNumberPanel:
Code:
lineNumberPanel.setLayout(new BoxLayout(lineNumberPanel,BoxLayout.Y_AXIS));
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 09-21-2008, 06:17 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
keffie91 is on a distinguished road
I tried to think of a method to remove the LineNumbers. But it is not so easy as i thought. Can you give me some hints

thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 09-22-2008, 07:11 AM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 356
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Have you implemented the adding numbering the lines in the text area?
Ok for removing you should again to create in the same class the method called removeUpdate(Document e) like this:

Code:
public void removeUpdate(DocumentEvent e) { int rows=textArea.getLineCount(); lineNumberPanel.removeAll(); for(int i=1;i<=rows;i++){ JLabel lineCount=new JLabel(""+i); lineCount.setFont(textArea.getFont()); lineNumberPanel.add(lineCount); } lineNumberPanel.updateUI(); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JAVA Fast Track Course fortius_computers Reviews / Advertising 0 08-30-2008 11:55 AM
How to track client logout time and orignal ipaddress (not gateway) in java psandeep JavaServer Pages (JSP) and JSTL 1 06-13-2008 02:32 AM
How to manipulate JtextArea bachtoutou New To Java 8 05-22-2008 11:36 AM
JTextArea saytri New To Java 0 01-13-2008 03:07 AM
Track download nilesh.malode Advanced Java 1 07-13-2007 11:44 PM


All times are GMT +3. The time now is 09:36 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org