🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
11 messages
11 total messages Started by Algis Kabaila Thu, 27 Nov 2003 01:21
jTextArea component - help an old newbee!!!
#1458
Author: Algis Kabaila
Date: Thu, 27 Nov 2003 01:21
46 lines
2738 bytes
Hi,
Though I am 78, I am a newbee in JBuilder, Swing components, Java and C, so
I need some very basic help.

I want to port some of my old Delphi (vers 5) programs to public domain,
incl. Linux.  One way of doing it is to do a re-write in Java and that is
what I am attempting to do.

To the "SimpleTextEditor" example of JBuilder Personal, I added a button and
the following code to see how JTextArea component works.  My Delphi program
reads the data from the text component, processes it and then attaches the
results at the end of the text component. This ensures that the data and the
corresponding output are at the same place.

To the "SimpleTextEditor" example of JBuilder Personal, I added a button and
the following code to see how JTextArea component works.  I have not
attempted to do any processing of it - just an echo check of data.  (I don't
know how to handle exceptions in Java, so I fudged the code for the catch
clause).

// Al added this method to see how the jTextArea1 component works.
  void jBTest_actionPerformed(ActionEvent e) {
file://Read text from the JTextArea1 and append at the end of it.
    int len = 0;
    int line = 0;
    int lineCount = jTextArea1.getLineCount();
    jTextArea1.append("\n\n ---------------------------------------------\n"
);
    jTextArea1.append("Appended text\n");
    for (line=0;line<lineCount;line++) {
      try{
        len = jTextArea1.getLineEndOffset(line) -
        jTextArea1.getLineStartOffset(line) + 1;
        jTextArea1.append(jTextArea1.getText(line,len));
      }
      catch(Exception g ){
        jTextArea1.append("\n Bad location exception occurred");
      }
    }
  }

The output is a mixture of the first and the second line of entered text.  I
find it weird and don't see what is going on.  Any suggestions would be
greatly appreciated (even if you just tell me off...).

Thanks, Al.
Re: jTextArea component - help an old newbee!!!
#1459
Author: Algis Kabaila
Date: Thu, 27 Nov 2003 01:30
20 lines
1567 bytes
The 3-rd line of the code has been amended in the process of copying it to a
newsgroup. In the original listing the beginning of the code looks as
follows:

> // Al added this method to see how the jTextArea1 component works.
>   void jBTest_actionPerformed(ActionEvent e) {
>// Read text from the JTextArea1 and append at the end of it.
>     int len = 0;
>     int line = 0;
>     int lineCount = jTextArea1.getLineCount();
>
    jTextArea1.append("\n\n ---------------------------------------------\n"
(snip...- the rest of the code copied correctly)

Sorry about it - newbee is a newbee, including a newbee in this mailing
list.

Thank you for your patience,

Al.
Re: jTextArea component - help an old newbee!!!
#1460
Author: Algis Kabaila
Date: Thu, 27 Nov 2003 01:51
26 lines
1185 bytes
Hi,

I failed to say that I am currently using Win2K as the platform.  Here is a
sample of input and output (under the header of "Appended text")

Thanks for looking at it!

Al.

1
22
333
4444



 ---------------------------------------------
Appended text
1
2
22
22
332
333

333
Re: jTextArea component - help an old newbee!!!
#1462
Author: Paul Furbacher
Date: Fri, 28 Nov 2003 17:42
146 lines
5482 bytes
Algis Kabaila wrote:


> I failed to say that I am currently using Win2K as the platform.  Here is a
> sample of input and output (under the header of "Appended text")
> 
> Thanks for looking at it!

You have given us the code, and some sample
output which doesn't fit your expectations,
but I'm left scratching my head as to what
you actually expect.  What are you trying to
do?  What does successful output look like?

Also, why is it necessary to dump the processed
text back into the same component?  Does it
make sense that the user could process some
text, and then press the button to process the
processed text?  (That's what this design suggests.)
Usually, input is separated from output.  Output
is usually set into a display component which
does not allow editing or processing.

Given that you are moving your Delphi based
code to Java, it might be a suitable moment
to reconsider this conflicting use of an
input area as an output device.  Here's
an alternative:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleTextEditorExampleFrame
     extends JFrame {

   JScrollPane scroller1 = new JScrollPane();
   JScrollPane scroller2 = new JScrollPane();
   JTextArea jTextArea1 = new JTextArea();
   JTextArea jTextArea2 = new JTextArea();
   JButton processButton = new JButton();
   GridBagLayout gridBagLayout1 = new GridBagLayout();


   public SimpleTextEditorExampleFrame() {
     try {
       jbInit();
     }
     catch (Exception ex) {
       ex.printStackTrace();
     }
   }

   void jbInit() throws Exception {
     this.getContentPane().setLayout(gridBagLayout1);
     processButton.setText("Process");
     processButton.addActionListener(
       new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
           processButton_actionPerformed(e);
         }
       });
     jTextArea1.setText("");

     jTextArea2.setEditable(false);
     jTextArea2.setBackground(Color.lightGray);
     jTextArea2.setText("");

     this.addWindowListener(
        new java.awt.event.WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            this_windowClosing(e);
          }
        });
     scroller1.getViewport().add(jTextArea1, null);
     scroller1.setPreferredSize(new Dimension(300, 150));
     scroller2.getViewport().add(jTextArea2, null);
     scroller2.setPreferredSize(new Dimension(300, 150));

     this.getContentPane().add(
       scroller1,
       new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
         GridBagConstraints.NORTHWEST,
         GridBagConstraints.BOTH,
         new Insets(12, 12, 11, 11), 0, 0));
     this.getContentPane().add(
       scroller2,
       new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
         GridBagConstraints.NORTHWEST,
         GridBagConstraints.BOTH,
         new Insets(0, 12, 0, 11), 0, 0));
     this.getContentPane().add(
       processButton,
       new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
         GridBagConstraints.CENTER,
         GridBagConstraints.NONE,
         new Insets(12, 0, 11, 0), 0, 0));
   }

   public static void main(String[] args) {
     SimpleTextEditorExampleFrame frame =
         new SimpleTextEditorExampleFrame();
     frame.pack();
     frame.setVisible(true);
   }

   void processButton_actionPerformed(ActionEvent e) {
     try {
       jTextArea2.setText(jTextArea1.getText());
       jTextArea2.append("\n\n ----\n");
       jTextArea2.append(
           "Line count: " +
           String.valueOf(jTextArea1.getLineCount()) + "\n");
       jTextArea2.append(
           "Character count: " +
           String.valueOf(jTextArea1.getText().length()) + 
"\n");
       jTextArea2.append("----");
     }
     catch (Exception g) {
       jTextArea2.setText("\n Bad location exception occurred");
     }
   }

   void this_windowClosing(WindowEvent e) {
     System.exit(0);
   }
}


-- 


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.
Re: jTextArea component - help an old newbee!!!
#1466
Author: Algis Kabaila
Date: Sun, 30 Nov 2003 10:55
51 lines
3168 bytes
Paul Furbacher wrote:

> You have given us the code, and some sample
> output which doesn't fit your expectations,
> but I'm left scratching my head as to what
> you actually expect.  What are you trying to
> do?  What does successful output look like?

First and foremost - thank you for replying.  It is greatly appreciated.
I also want to appologise for two things:

Firstly, for not responding more promptly.  My excuse is that in addition to
looking at new (for me, that is) programming languages, I am also in the
process of moving from Win to Linux platform.

Secondly, for failing to explain what I am trying to do.  My failure is
particularly bad, since I spent my lifetime explaining technical matter to
younger people.  I will try to rectify that failure:

1. I want to read lines from the jTextArea1 component as 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...

2. I want to process the data and write it back on the same jTextArea1
component.  As the first step of testing that procedure, I want to echo
check the data by dumping it back onto the comoponent.

It may look strange that the I plan the output to go back on the same screen
as the input.  Actually, there is a good reason for wanting to do that.  As
an old structural engineer (with a healthy share of structural
failures...), I know that most failures occur not because of having the
right answers, but because of failure to communicate.  One area of such
failure is to give the "right answers" to the wrong problem!  Hving the
input and output on the same component reduces the chances of "right answer
to the wrong problem" syndrome.

Of course, it is nice to start with some of the work already done for you -
I see no better way than to use an existing sample editor!  I have gone
through the tutorial to become familiar with it and to produce a "new"
code, being conscious of the copyrignt notice on the example.

Thanks again for your response - greatly appreciated.

Sincerely,

Al.

(polites snip...)
-- 
Al. Kabaila
Re: jTextArea component - help an old newbee!!!
#1470
Author: Paul Furbacher
Date: Sun, 30 Nov 2003 18:14
63 lines
3434 bytes
Algis Kabaila wrote:

> 1. I want to read lines from the jTextArea1 component as
> 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...
> 
> 2. I want to process the data and write it back on the
> same jTextArea1 component.  As the first step of testing
> that procedure, I want to echo check the data by dumping
> it back onto the comoponent.

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.


On a side issue:

> Of course, it is nice to start with some of the work
> already done for you - I see no better way than to use an
> existing sample editor!  I have gone through the tutorial
> to become familiar with it and to produce a "new" code,
> being conscious of the copyrignt notice on the example.

Yes, and no.  The editor may in fact be far too
complicated, with features which get in the way,
rather than help.  I often use the samples as springboards
for testing ideas, but just as often, I'll start with
a blank slate.  The latter approach lets me concentrate
on just the issues related to a specific component.
Reducing the number of side effects which a complex
example may introduce can potentially save time.
But there's no "this way is the best way" in this
matter.


-- 


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.
Re: jTextArea component - help an old newbee!!!
#1471
Author: Algis Kabaila
Date: Mon, 01 Dec 2003 06:08
119 lines
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");
� � � �}
� � �}
� �}
Re: jTextArea component - help an old newbee!!!
#1477
Author: Paul Furbacher
Date: Mon, 01 Dec 2003 14:30
122 lines
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.
Re: jTextArea component - help an old newbee!!!
#1475
Author: Algis Kabaila
Date: Mon, 01 Dec 2003 18:29
72 lines
3947 bytes
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, which is of the type Document (line 61 of
code).  The document1 is also used to "listen" to text changes (line 118)
and also appears in lines 399, 406 etc.  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?
 
> 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? 
JArea1 component does not know about it. "document1" does not know about it
either - compiler tells me that.   So how can I use it?  Most of the
methods that I can access in document1 relate to individual characters, and
not lines of text.

It is the lines of text that I would like to acess as strings. I would then
extract the numeric data from it, which, after all, constitutes 99% of the
data.

> Using this method of getting each line obviates
> the need to calculate offsets, etc., and may
> make you code more readable.

Absolutely yes!  But how to do it?

> On a side issue:
> 
>> Of course, it is nice to start with some of the work
>> already done for you - I see no better way than to use an
>> existing sample editor!  I have gone through the tutorial
>> to become familiar with it and to produce a "new" code,
>> being conscious of the copyrignt notice on the example.
> 
> Yes, and no.  The editor may in fact be far too
> complicated, with features which get in the way,
> rather than help.  I often use the samples as springboards
> for testing ideas, but just as often, I'll start with
> a blank slate.  The latter approach lets me concentrate
> on just the issues related to a specific component.
> Reducing the number of side effects which a complex
> example may introduce can potentially save time.
> But there's no "this way is the best way" in this
> matter.

Actually, I think that the "Simple Editor" is of about the right complexity.
In fact, I would add a facility of changing the fonts - important for sight
impaired and older users.

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.

Thanks again,

Al.


-- 
Al. Kabaila
Re: jTextArea component - help an old newbee!!!
#1478
Author: Algis Kabaila
Date: Sat, 06 Dec 2003 00:27
24 lines
2040 bytes
I have been away from this newsgroup for a little while, but longer than I
intended.  I was inundated with spam and "Sven" virus messages.  Clearly,
this sudden increase in the undesirable mail was due to me returning to
newsgroups.  One of the servers that I use requires that real email
addresses be used in all news groups.  So I now switched to another
(valid!) email address at yahoo.com. My old email address is no longer
valid :-(

I would like to continue the discussion in this thread.  Whilst it is a
dialog, rather than wider conversation, it has tought me a lot!

However, it has developed into two topices - 

1. The use of jTextArea component for program input.
2. Merits and pitfalls of using the same component for both input and
output.

I plan to edit the comments into two groups and post it as new threads, if
that is acceptable.  Please let me know if it is NOT acceptable - I am very
conscious of netiquette in this usenet group.  Also, if you have followed
the discussion, would you indicate that you are/are not interested?

-- 
Al. Kabaila
Re: jTextArea component - help an old newbee!!!
#1479
Author: Algis Kabaila
Date: Sat, 06 Dec 2003 00:35
4 lines
1035 bytes
I failed to get my new and current email address to show...

-- 
Al. Kabaila
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads