Thread View: comp.lang.java.gui
6 messages
6 total messages
Started by Paul King
Thu, 22 Mar 2001 00:22
A very humble Java GUI question
Author: Paul King
Date: Thu, 22 Mar 2001 00:22
Date: Thu, 22 Mar 2001 00:22
37 lines
2737 bytes
2737 bytes
I am not necessarily new to Java, but am a novice at writing GUI-based
Java programs.
I am trying to build a GUI with a text object (JTextPane). I have a need
to place display text into that object. Here is the declaration:
javax.swing.JTextPane txtMessage = new javax.swing.JTextPane();
Having done that, in the same scope as the current object that the above
declaration was in, I placed this statement in one of the methods, a
response to pressing a button:
txtMessage.text = "Some text";
hoping it would appear as displayed text in the text pane. The logic of
using txtMessage.text is that the "text" part appears as a property on my
CodeWarrior object inspector for the "txtMessage" object. Giving the
"text" property a value in the object inspector results in that value
being displayed.
However, expressing the object as I have above results in CodeWarrior
telling me that the property "text" does not exist as a part of
javax.swing.JTextPane. Indeed, it does not appear to appear in the API for
JTextPane nor any of the classes it implements that I have seen. So, where
is the Object inspector assigning the value? In object inspectors of other
languages I've written in, the properties listed there are properties of
the class that is "inspected". Usually, a simple assignment statement like
the one above should work. However, it does not compile, because "text" is
apparently not a property of JTextPane. Is CodeWarrior trying to screw
with my mind?
If I am going about this all wrong, can anyone suggest how I can write
text into a GUI-based text object?
Thanks for any help you can offer.
PJK
Re: A very humble Java GUI question
Author: Bill Tschumy
Date: Thu, 22 Mar 2001 03:01
Date: Thu, 22 Mar 2001 03:01
50 lines
3199 bytes
3199 bytes
On Wed, 21 Mar 2001 18:22:58 -0600, Paul King wrote
(in message <rihibt42piiu8mbei09uc7o066ll4sramp@4ax.com>):
> I am not necessarily new to Java, but am a novice at writing GUI-based
> Java programs.
>
> I am trying to build a GUI with a text object (JTextPane). I have a need
> to place display text into that object. Here is the declaration:
>
> javax.swing.JTextPane txtMessage = new javax.swing.JTextPane();
>
> Having done that, in the same scope as the current object that the above
> declaration was in, I placed this statement in one of the methods, a
> response to pressing a button:
>
> txtMessage.text = "Some text";
>
> hoping it would appear as displayed text in the text pane. The logic of
> using txtMessage.text is that the "text" part appears as a property on my
> CodeWarrior object inspector for the "txtMessage" object. Giving the
> "text" property a value in the object inspector results in that value
> being displayed.
>
> However, expressing the object as I have above results in CodeWarrior
> telling me that the property "text" does not exist as a part of
> javax.swing.JTextPane. Indeed, it does not appear to appear in the API for
> JTextPane nor any of the classes it implements that I have seen. So, where
> is the Object inspector assigning the value? In object inspectors of other
> languages I've written in, the properties listed there are properties of
> the class that is "inspected". Usually, a simple assignment statement like
> the one above should work. However, it does not compile, because "text" is
> apparently not a property of JTextPane. Is CodeWarrior trying to screw
> with my mind?
>
> If I am going about this all wrong, can anyone suggest how I can write
> text into a GUI-based text object?
>
> Thanks for any help you can offer.
>
> PJK
I don't have any advice about why CodeWarrior is showing that
property, but to set the text displayed you should do a
txtMessage.setText("Some Text");
--
Bill Tschumy
Otherwise -- Austin, TX
bill@otherwise.com
Re: A very humble Java GUI question
Author: Stephen Ostermiller
Date: Thu, 22 Mar 2001 10:21
Date: Thu, 22 Mar 2001 10:21
8 lines
1336 bytes
1336 bytes
txtMessage.setText("Some text");
Find the Java APIs from java.sun.com, they are great for answering
this type of question.
Stephen
Java utilities: CSV parsing, StringTokenizer replacement, password dialog, Base64 encode/decode, and web browser control.
http://www.smo.f2s.com/utils/
Re: A very humble Java GUI question
Author: gbp
Date: Fri, 23 Mar 2001 06:29
Date: Fri, 23 Mar 2001 06:29
40 lines
2446 bytes
2446 bytes
>However, expressing the object as I have above results in
CodeWarrior
>telling me that the property "text" does not exist as a part of
>javax.swing.JTextPane. Indeed, it does not appear to appear in the
API for
>JTextPane nor any of the classes it implements that I have seen.
Wow did you IDE lie to you! Don't feel bad mine lies too.
There is some decent material on the Text objects at
www.javasoft.com.
I'll give you the summary:
* Java's text objects are a pain to use (you won't find that at
Sun's site) but are powerfull.
* The larger text objects store their text in a Document object.
You can get the Document object by using JTextPane.getDocument()
* This is the key THE BEST WAY TO CHANGE THE TEXT IS BY USING THE
DOCUMENT OBJECT NOT THE setText method. setText is slow and doesn't
draw nicely on the screen.
* The Document object is kinda a pain too!
* Use the document's insertString method.
* so....
myJPane.getDocument.insertString("my stuff", offset, null);
the first argument is a string. the second is the index (you can
get this for other methods of Document) The last argument is the
Attibute you want the text in (null seems to work just fine!);
Neat Trick:
You can tie one Document object to as many Swing Text Components as
you want (or zero if you want).
Re: A very humble Java GUI question
Author: Jens Gohrke
Date: Fri, 23 Mar 2001 08:07
Date: Fri, 23 Mar 2001 08:07
40 lines
2912 bytes
2912 bytes
Paul King wrote: > I am not necessarily new to Java, but am a novice at writing GUI-based > Java programs. > > ... > > txtMessage.text = "Some text"; > > hoping it would appear as displayed text in the text pane. The logic of > using txtMessage.text is that the "text" part appears as a property on my > CodeWarrior object inspector for the "txtMessage" object. Giving the > "text" property a value in the object inspector results in that value > being displayed. Well, sorry - but "did you hear about beans??". With a bean there might be a property 'text' and access to it is granted using accessor methods 'getText' and/or 'setText'. this is a common pattern to all beans - and Swing components are supposed to be beans, too. No Java style guide misses a phrases stating that the above way (direct access to class members) is to be dropped from coding practices. Well, I don't think that it is that seriously wrong, but you might consider the accessor way - that btw is the way that (almost) every IDE GUI Tool should do it by recommendation. btw. i agree to the other posting by "gbp" about using the getDocument() method for components like JTextPane as JTextPane may encapsulate large text objects - highlighting and other stuff - like Win$ Write.exe... given proper coding ;-) but the text property is not isolated to JTextPane and if you would like to access anything else having a text property (eg. JButton) please stick to the accessor. and also the accessor might hide implementations to update components in the background by calling whatever is necessary to do so. accessing the member directly would need you to do so and thus involve dependencies you might not be aware of --- the saying goes: use accessors where available... Cheers Jens
Re: A very humble Java GUI question
Author: Bryan E. Sampieri
Date: Fri, 23 Mar 2001 16:00
Date: Fri, 23 Mar 2001 16:00
38 lines
2201 bytes
2201 bytes
Bill Tschumy wrote:
> On Wed, 21 Mar 2001 18:22:58 -0600, Paul King wrote
> (in message <rihibt42piiu8mbei09uc7o066ll4sramp@4ax.com>):
>
> > I am trying to build a GUI with a text object (JTextPane). I have a need
> > to place display text into that object. Here is the declaration:
> >
> > javax.swing.JTextPane txtMessage = new javax.swing.JTextPane();
> >
> > Having done that, in the same scope as the current object that the above
> > declaration was in, I placed this statement in one of the methods, a
> > response to pressing a button:
> >
> > txtMessage.text = "Some text";
> >
>
> >
> > PJK
>
> I don't have any advice about why CodeWarrior is showing that
> property, but to set the text displayed you should do a
>
> txtMessage.setText("Some Text");
>
> --
> Bill Tschumy
> Otherwise -- Austin, TX
> bill@otherwise.com
Probably it's showing all fields, of which "text" would probably be the private
or protected internal field to hold the string that is the text for the
textfield.
--
Bryan E. Sampieri
bryans@avaya.com
The opinions expressed are not necessarily those of Avaya.
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