Article View: borland.public.jbuilder.java.swing
Article #1471Re: jTextArea component - help an old newbee!!!
From: Algis Kabaila
Date: Mon, 01 Dec 2003 06:08
Date: Mon, 01 Dec 2003 06:08
119 lines
5806 bytes
5806 bytes
First and foremost, I want to express my sincere appreciation to Paul
Furbacher and Team B for responding to my question. You must be and you are
incredibly patient - newbie questions could be answered if only they read
the documentation! And I am a typical newbie!
Paul Furbacher wrote:
> Algis Kabaila wrote:
(snip...)
>> strings. I am trying to use getText(int offs,int
>> len)function. Oops - I think I can see where I goofed:
>> (in offs) probably referst to offset in bytes, not the
>> line number...
(snip...)
That was indeed a coding mistake. I have corrected it and ran the program
again with the (hopefully) correct offset from the start of the text area
in bytes, named "offs" in the program snipped. The first line is reproduced
correctly, but in the second and subsequent lines the first character of
the line is reproduced twice. I have tried the same program and the same
code under Linux and the results are the same. I think one can say that
the component has a bug and one has to look for a workaround.
> Any model for a JTextArea should implement Document.
> The default model for JTextArea is
> javax.swing.text.PlainDocument.
>
> Since it looks as though you are going to use
> a very simple document structure, you might
> want to take advantage of the default document's
> "getParagraphElement(int pos)" method. The
> default model is line-based, so "pos" is the
> line number.
>
> Using this method of getting each line obviates
> the need to calculate offsets, etc., and may
> make you code more readable.
This is a very valuable advice and I will certainly try it. It seems much
simpler than calculation of the offset and corresponds to what I planned to
do. I have not tried it yet, as I stubbornly want to find out why "my"
approach did not work. I will try to use the "getParagraphElement(int
pos)" method and will report the results here - mainly in a manner of
discussion with myself :-)
Thanks again for replying. �It is a wonderful support and
help to decide what software/language to use. �I am trying JBulider9,
personal (free) edition.
Let me summarise where I am at this stage:
I have corrected one mistake in the program, where I was using line offset
(or line number) rather than the offset of the beginning of the line in
bytes. I entered the data avoiding duplicate characters on any line. I
entered three lines of text:
First line : 123
Second line: abcd
Third line:efghilkl.
I then clicked on the button, which I added on the tool bar and which calls
the my inserted method to read-write on the component. The program
reproduced the first line correctly, but in the following two lines it
produced the first character TWICE. �It seems to me that there is some kind
of timing problem, or some problem with the Win end-of-line handling, or
some buffering problem.
I am hoping that the process of �reading from the jTextArea1 �is correct and
that if I did delay between line-reads, as in fact it will _usually_ (but
not necessarily always) happen in the actual data processing, the line
reading would work OK. � But it would be better to know how the component
works. As I have already said, I will try the simpler alternative method
suggested in Paul Furbacher's reply, but I want to be know more how the
component works.
Thanks for your help so far and any further insight into this component
behaviour.
Sincerely,
Al.
PS: attached is the input and the output and the listing of the code called
by the additional button inserted on the toolbar of the Simple Text Editor.
123
abcd
efghijkl
� ---------------------------------------------
Appended text
123
aabcd
eefghijkl
***********************
Program listing follows:
***********************
// Al added this method to see how the jTextArea1 component works.
// Read text from the JTextArea1 and append at the end of it.
� �void jBTest_actionPerformed(ActionEvent e) {
� � �int len = 0;
� � �int line = 0;
� � �int lineCount = jTextArea1.getLineCount();
� � �int offs = 0;
� � �jTextArea1.append("\n\n ------------------------------------------\n");
� � �jTextArea1.append("Appended text\n");
� � �for (line=0;line<lineCount;line++) {
� � � �try{
� � � � �offs = jTextArea1.getLineStartOffset(line);
� � � � �len = jTextArea1.getLineEndOffset(line) - offs + 1;
� � � � �jTextArea1.append(jTextArea1.getText(offs,len));
� � � �}
� � � �catch(Exception g ){
� � � � �jTextArea1.append("\n Bad location exception occurred");
� � � �}
� � �}
� �}
Technical Details
Message-ID:
<bqdhml$20u792$1@ID-216608.news.uni-berlin.de>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!ta-1-28.tip.net.AU!not-for-mailReferences:
<bq3lia$1tiqfa$1@ID-216608.news.uni-berlin.de> <bq3n98$1ublqa$1@ID-216608.news.uni-berlin.de> <3fc78900$1@newsgroups.borland.com> <bqbe3a$1pq2v0$1@ID-216608.news.uni-berlin.de> <3fca3399$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,663e7dcc14de267d X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2003-11-30 11:50:48 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!ta-1-28.tip.net.AU!not-for-mail From: Algis Kabaila <akabaila@pcug.org.au> Newsgroups: borland.public.jbuilder.java.swing Subject: Re: jTextArea component - help an old newbee!!! Date: Mon, 01 Dec 2003 06:08:41 +0000 Organization: PC Users Group Lines: 119 Message-ID: <bqdhml$20u792$1@ID-216608.news.uni-berlin.de> References: <bq3lia$1tiqfa$1@ID-216608.news.uni-berlin.de> <bq3n98$1ublqa$1@ID-216608.news.uni-berlin.de> <3fc78900$1@newsgroups.borland.com> <bqbe3a$1pq2v0$1@ID-216608.news.uni-berlin.de> <3fca3399$1@newsgroups.borland.com> Reply-To: akabaila@pcug.org.au NNTP-Posting-Host: ta-1-28.tip.net.au (203.11.71.28) Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8Bit X-Trace: news.uni-berlin.de 1070221846 68099362 203.11.71.28 ([216608]) User-Agent: KNode/0.7.2