Results 1 to 14 of 14
- 09-18-2008, 05:23 PM #1
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
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?
keffie91Never give up! ;)
- 09-18-2008, 07:17 PM #2
Of course it is possible,examine my editor ,which i wrote this year SourceForge.net: Sampad
If you need the source code,tell me.
- 09-18-2008, 09:06 PM #3
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.
- 09-18-2008, 11:02 PM #4
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.
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):Java 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();
3.removes evertyhing from your LineNumbers panel (supposing that LineNumber track is JPanel lineNumberPanel):Java Code:int rows=textArea.getLineCount();
4.Add again labels on the line number count panel and repaint it:Java Code:lineNumberPanel.removeAll();
5.the same you can do when you remove the line:Java Code:for(int i=1;i<=rows;i++){ JLabel lineCount=new JLabel(""+i); lineCount.setFont(textArea.getFont()); lineNumberPanel.add(lineCount); } lineNumberPanel.updateUI(); }
I hope you understood.WelcomeJava Code:public void removeUpdate(DocumentEvent e) {}
- 09-19-2008, 07:17 AM #5
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
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 keffie91Last edited by keffie91; 09-19-2008 at 02:11 PM.
Never give up! ;)
- 09-19-2008, 04:02 PM #6
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:
Java Code:lineNumberPanel.setPreferredSize(new Dimension(15, textArea.getHeight())); lineNumberPanel.setBackground(Color.gray); lineNumberPanel.setLayout(new BoxLayout(lineNumberPanel,BoxLayout.Y_AXIS));
- 09-19-2008, 04:31 PM #7
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
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 keffie91Never give up! ;)
- 09-19-2008, 04:45 PM #8
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.
- 09-19-2008, 04:49 PM #9
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
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 keffie91Never give up! ;)
- 09-19-2008, 04:57 PM #10
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.
- 09-19-2008, 06:23 PM #11
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
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 keffie91Never give up! ;)
- 09-19-2008, 06:28 PM #12
adjust the layout for the lineNumberPanel:
Java Code:lineNumberPanel.setLayout(new BoxLayout(lineNumberPanel,BoxLayout.Y_AXIS));
- 09-21-2008, 04:17 PM #13
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
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 keffie91Never give up! ;)
- 09-22-2008, 05:11 AM #14
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:
Java 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(); }
Similar Threads
-
JAVA Fast Track Course
By fortius_computers in forum Reviews / AdvertisingReplies: 0Last Post: 08-30-2008, 09:55 AM -
How to track client logout time and orignal ipaddress (not gateway) in java
By psandeep in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-13-2008, 12:32 AM -
How to manipulate JtextArea
By bachtoutou in forum New To JavaReplies: 8Last Post: 05-22-2008, 09:36 AM -
JTextArea
By saytri in forum New To JavaReplies: 0Last Post: 01-13-2008, 01:07 AM -
Track download
By nilesh.malode in forum Advanced JavaReplies: 1Last Post: 07-13-2007, 09:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks