Article View: borland.public.jbuilder.java.swing
Article #1477Re: jTextArea component - help an old newbee!!!
From: Paul Furbacher
Date: Mon, 01 Dec 2003 14:30
Date: Mon, 01 Dec 2003 14:30
122 lines
5559 bytes
5559 bytes
Algis Kabaila wrote:
> Paul Furbacher wrote:
>>Any model for a JTextArea should implement Document.
>>The default model for JTextArea is
>>javax.swing.text.PlainDocument.
>
>
> Yes, in the SimpleTextEditor tutorial example the
> jTextArea1 is related (line 82 of code) to document1,
> [...] Strange, I did the tutorial twice, but do not
> remember any instructions to create or use document1 of
> type Document. Is its creation one bit of magic
> performed by JBuilder?
No, there's no magic. Swing components are designed
around the "model-view-controller" paradigm. Each
component has a "model", i.e., the data which it
displays. I'd suggest that you visit the Java Tutorial
site to begin learning about MVC and Swing. The
following page addresses the issue of models directly:
http://java.sun.com/docs/books/tutorial/uiswing/components/model.html
>>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.
>
>
> Yes, my handling of the input is very simple indeed. And
> "getParagraphElement(int pos)" is the very method that
> corresponds to the Delphi text components method of
> reading lines. But how do I implement it? [...]
Actually, forget what I wrote. I didn't look
closely enough at JTextArea's methods which
you are using: they essentially use the Document
methods I was referencing. There's no advantage
to directly referencing them yourself. The
above was just bad advice on my part, the result
of putting mouth in gear just a bit too fast.
> Of course, from a programmers view point, the
> input-output separation seems desirable and logical.
> From a user's viewpoint, I think the opposite is true.
Actually, the programmer's perspective is irrelevant
to user interface design. The only perspective
which matters is that of the user. I was speaking
from the latter perspective. I could care less
whether it's easier for me to program one way or
the other. I based my assessment on some 15+ years
of experience designing user interfaces, and the
reading I've done on this matter of those years.
I'd venture to say that structural engineers are no
different from all other users. Input mixed with
output is potentially confusing. In fact, it
has confused both you and me while trying to
analyze what your code was doing. For a while,
I couldn't see what was throwing your output
out of whack. But take a look at how your code
modifies the input _before_ it starts to analyze
it!
...
jTextArea1.append("\n\n-----------\n");
jTextArea1.append("Appended text\n");
... // After adding the above text to
// the buffer, you then start analyzing all
// of it.
If you were dumping the output to a separate
output control, you wouldn't be affecting the
original data. And your user would see that
there's a separation between what he or she
has entered and what analysis has produced
from that input.
To remedy the problem (but still re-use your text
area), dump the analysis to a StringBuffer, and
only when done with the analysis, append the result
to the text area:
// Note: "ta" is your JTextArea component; just saving
// myself some typing with that and the ellipses.
void doTextAnalysis(ActionEvent e) {
int count = ta.getLineCount();
StringBuffer b = new StringBuffer();
b.append("\n\n----\nAppended text\n");
for ( int line = 0; line < count; line++ ) {
try {
int start = ta.getLineStartOffset(line);
int end = ta.getLineEndOffset(line) - start;
b.append(ta.getText(start, end));
}
catch ( ... ) {...}
}
ta.append(b.toString());
}
--
Paul Furbacher (TeamB)
Save time, search the archives:
http://www.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Technical Details
Message-ID:
<3fcb506b$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!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> <bqet3f$21fqeh$1@ID-216608.news.uni-berlin.de>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,663e7dcc14de267d X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2003-12-01 06:39:25 PST Date: Mon, 01 Dec 2003 09:30:07 -0500 From: Paul Furbacher <pfurbacher@mac.com> User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.5) Gecko/20031013 Thunderbird/0.3 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: jTextArea component - help an old newbee!!! 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> <bqet3f$21fqeh$1@ID-216608.news.uni-berlin.de> In-Reply-To: <bqet3f$21fqeh$1@ID-216608.news.uni-berlin.de> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 68.39.103.120 Message-ID: <3fcb506b$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1070289003 68.39.103.120 (1 Dec 2003 06:30:03 -0700) Lines: 123 Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail