Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2008

Tip: Looking for answers? Try searching our database.

How to display text messages in a scrolling text area?

Thread view: 
Stefan Ram - 15 May 2008 19:42 GMT
To display text messages (as - for example - in a window with
 log messages) in a scrolling text area, there are several
 Swing components I am aware of:

http://download.java.net/jdk7/docs/api/javax/swing/JTextArea.html
http://download.java.net/jdk7/docs/api/javax/swing/JTextPane.html
http://download.java.net/jdk7/docs/api/javax/swing/JEditorPane.html

 I might want to subclass one of them for my »log area«. While
 I only think of plain text output now, more features, such as
 different typefaces or text colors might come in handy later.

 I also might want to use one of them in teaching, when showing
 how to display log messages in a GUI.

 It seems that JEditorPane has more features than the other two.
 So should I always use JEditorPane for such purposes, or are there
 any reasons to use one of JTextArea or JTextPane sometimes?
Jeff Higgins - 16 May 2008 09:13 GMT
>  To display text messages (as - for example - in a window with
>  log messages) in a scrolling text area, there are several
[quoted text clipped - 7 lines]
>  I only think of plain text output now, more features, such as
>  different typefaces or text colors might come in handy later.

What about tabular log data?
<http://download.java.net/jdk7/docs/api/javax/swing/JTable.html>

>  I also might want to use one of them in teaching, when showing
>  how to display log messages in a GUI.

Follow the lesson plan?
Is this CS2101 The Java Language, or CS2202 Java Swing GUIs

>  It seems that JEditorPane has more features than the other two.
>  So should I always use JEditorPane for such purposes, or are there
>  any reasons to use one of JTextArea or JTextPane sometimes?

JTextArea has .getLineCount()
JEditorPane has .getHyperlinkListeners()
JTextPane has  .insertComponent(Component c)
Stefan Ram - 16 May 2008 14:33 GMT
>Follow the lesson plan?
>Is this CS2101 The Java Language, or CS2202 Java Swing GUIs

 I am giving adult evening classes and am solely responsible
 for the lesson plan, so I do not have to follow anyone's
 lesson plan and can fit my own lesson plan to the interests
 and capabilities of the adult pupils of this adult education
 center.

 They are often beginners, and I do not have to treat a certain
 set of topics, so instead of giving an overview, I have chosen
 to teach just a few of the most fundamental aspects of the
 language, but then use enough time for each of those topics to
 do exercises until nearly everyone is able to apply it.

 My classes advance quite slowly and do cover only a small part
 of the language, but a high percentage of the pupils who
 attend the class will understand most of it, even if this is
 their first programming language.

 I will have a look at CS2101 and CS2202.
Jeff Higgins - 16 May 2008 19:20 GMT
>>Follow the lesson plan?
>>Is this CS2101 The Java Language, or CS2202 Java Swing GUIs
[quoted text clipped - 4 lines]
>  and capabilities of the adult pupils of this adult education
>  center.

Maybe I should have said, The example code and assignments you
provide your class should follow the lesson plan.  Do you want
to have your students learn how to transform logger output to
styled text so that it may displayed in a styled text component?
From my experience there is no greater hindrance to learning than
unfocused examples and assignments.  For a example of a piss-poor
and unfocused assignment see the Purse and Coins assignment in a
recent c.l.j.? group discussion.

>  They are often beginners, and I do not have to treat a certain
>  set of topics, so instead of giving an overview, I have chosen
[quoted text clipped - 8 lines]
>
>  I will have a look at CS2101 and CS2202.
Stefan Ram - 17 May 2008 15:15 GMT
>to have your students learn how to transform logger output to
>styled text so that it may displayed in a styled text component?

 I need to estimate which applications of GUIs and Swing are
 most common, to teach only these, because it would not make
 sense to start with components needed only rarely. I estimate
 that many GUI programs need to output text reports to the
 user. Therefore, I want to show how to do this using a
 multi-line text component.

 Since the time constraints only allow for a single multi-line
 text component, I wonder which of JTextArea, JTextPane, and
 JEditorPane to use for this purpose.
Stefan Ram - 16 May 2008 15:48 GMT
>JTextArea has .getLineCount()

 OK, thanks! Apart from this operation it seems that
 overall JEditorPane and JTextPane are more powerful and
 high-level and thus often more simple to use than JTextArea.

 So it seems that usually to display text one would prefer
 JEditorPane or JTextPane. I still would appreciate if someone
 knew even other examples than obtaining a count of lines
 for when to prefer JTextArea.
Jeff Higgins - 16 May 2008 16:45 GMT
>>JTextArea has .getLineCount()
>
>  OK, thanks! Apart from this operation it seems that
>  overall JEditorPane and JTextPane are more powerful and
>  high-level and thus often more simple to use than JTextArea.

quoting from The Java Tutorials - Using Text Components

(JEditorPane - JTextPane)... "are powerful and multi-faceted components
suitable for high-end needs, and offer more avenues for customization
than the other text components. Because they are so powerful and flexible,
styled text components typically require more initial programming to set
up and use.
One exception is that editor panes can be easily loaded
with formatted text from a URL, which makes them useful
for displaying uneditable help information."

>  So it seems that usually to display text one would prefer
>  JEditorPane or JTextPane. I still would appreciate if someone
>  knew even other examples than obtaining a count of lines
>  for when to prefer JTextArea.

Prefer JTextArea for multi-line unstyled text.
Stefan Ram - 17 May 2008 14:47 GMT
>than the other text components. Because they are so powerful and flexible,
>styled text components typically require more initial programming to set
>up and use.

 A JTextArea also has some more difficult aspects.

 For example, when one wants a line appended at the end to be
 visible (a common case), with a JTextArea in a JScrollpane,
 sometimes one needs to call »scrollRectToVisible« with the
 offset of the last line, so one needs to call
 »getLineEndOffset(line)«, which sometimes might throw a
 BadLocationException, which also needs to be handled.

 But such boilerplate code does not matter that much when one
 is subclassing to get a custom log text area component from
 a Swing text component, because it all can be written once
 and then be hidden behind methods of the custom log component.

>Prefer JTextArea for multi-line unstyled text.

 JTextPane and JEditorPane also allow multi-line unstyled text,
 and when subclassing to get a custom log text window,
 boilerplate code needs only be written once (actually just
 copied from tutorials), so the difficulty to use does not
 matter that much.

 So I wonder, whether from the point of visible properties of
 the product (what one observes when the program with the log
 window is running), there would ever be a reason to prefer
 JTextArea above JTextPane or JEditorPane.

 For example, if JTextPane would be known to be slower than
 JTextArea, this would be such a reason.
Roedy Green - 17 May 2008 16:38 GMT
>  To display text messages (as - for example - in a window with
>  log messages) in a scrolling text area, there are several
>  Swing components I am aware of:

Another way is with a JTree.  Your treemodel need not keep all
non-visible messages in RAM.

See http://mindprod.com/jgloss/jtree.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.