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 / December 2005

Tip: Looking for answers? Try searching our database.

Best Approach for putting words on a JPanel (with special requirements) [2nd attempted post]

Thread view: 
PaulSchrum - 28 Dec 2005 17:09 GMT
I am a newbie to Swing and I am trying to do something that is over my
head.  This is a "how do I . . ." question or maybe a "what is the best
way to . . . " question.

Can anyone advise me on my approach?  That is, am I on the right track
but just need to call such-and-such?  Or am I totally up the wrong tree
and need to use class JWhatever instead.  Here is my little story:

I want to place text on a JPanel (which is a member of a JScrollPane).
I want each word to be discrete entity, and I want to have absolute
control over the font, size, etc of the text, along with absolute
control of the position of the text on the JPanel.

My first attempt was just to .drawString the words on the JPanel and I
got that to work fine.  But now I need each word to be aware of
mouseOvers and mouseClicks, stuff like that.  Using .drawString they
are not.

So I amy now trying to implmenent the drawing and placing of each word
using JTextField.  I put my text in the JTextField, set the font, and
size it so that it is large enough to contain all of the text (Using a
TextLayout instance).  I then add the JTextField to the JPanel and
setVisible(true).

The problem is that I do not see anything via this JTextField
implementation.

Thanks in advance for your help.

- Paul Schrum
Thomas Hawtin - 28 Dec 2005 17:44 GMT
> I want to place text on a JPanel (which is a member of a JScrollPane).
> I want each word to be discrete entity, and I want to have absolute
[quoted text clipped - 5 lines]
> mouseOvers and mouseClicks, stuff like that.  Using .drawString they
> are not.

I can see two obvious routes you can take.

Carry on in the direction of your first attempt. Probably introduce your
own class for representing each word. Have mouse and mouse motion
listeners on the main component find the target word instance and call
appropriate methods on that.

Alternatively represent each word with a JLabel or custom component.
Give the panel a custom LayoutManager that positions each word component
as you wish.

BTW, if you are just going to override paintComponent and draw,
extending JComponent is more logical the JPanel. In either case you
should probably setOpaque(true).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

PaulSchrum - 29 Dec 2005 02:33 GMT
> Alternatively represent each word with a JLabel or custom component.
> Give the panel a custom LayoutManager that positions each word component
> as you wish.

Tom,

Thank you very much for  your response.

Can you point me in the right direction for knowing how to give the
panel a custom LayoutManager?  An online tutorial or sample code or
something?

- Paul
PaulSchrum - 29 Dec 2005 02:39 GMT
Tom,

I thought of this after I posted my other response.

The reason I decided to avoid JLabel earlier is because of the
following statement in the API documentation for it:

"A label does not react to input events."

I suppose if I subclass it I can add mouse-sensitivity to my new class.
(Yes, my ignorance is showing, now.)

- Paul
Andrew Thompson - 29 Dec 2005 02:54 GMT
> The reason I decided to avoid JLabel earlier is because of the
> following statement in the API documentation for it:
[quoted text clipped - 3 lines]
> I suppose if I subclass it I can add mouse-sensitivity to my new class.
>  (Yes, my ignorance is showing, now.)

Vis.
<http://groups.google.com/group/comp.lang.java.gui/msg/c7fac340b5f27c6b>

HTH

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew

PaulSchrum - 29 Dec 2005 03:52 GMT
Andrew,

Thanks for this link.  After glancing at it I decided to save it for
further study.  (It is almost 11 PM here and I am in no mental state to
study stuff like that in detail right now.)

- Paul
Andrew Thompson - 29 Dec 2005 03:08 GMT
..
> I want each word to be discrete entity, and I want to have absolute
> control over the font, size, etc of the text, along with absolute
> control of the position of the text on the JPanel.

Where are you going to put it?  Describe it in words.
Some examples...

One after the other, then wrapping down the panel.. (FlowLayout)
In a vertical column of equally sized areas down the panel (GridLayout)
Scrolling from left to right across the panel each in turn
(custom layout 'ScrollLayout')
Wherever the user 'drops' it (custom layout - must account
for components overlapping 'DropLayout/AbsoluteLayout')

The thing is, to make a custom layout, you need to
translate those words to Java code, and as soon as you
can express it in words, the Java code for the custom
layout becomes (relatively) easy.

> My first attempt was just to .drawString the words on the JPanel and I
> got that to work fine.  

If you know where to 'drawString()' you can position the label
at the same poistion, though it usually makes more sense to
encapsulate the logic behind the drawString co-ordinates in
your custom layout.

Having said that, here is a very simple example of a ScrollLayout,
though note that one of the true GUI gurus was advising me
that it could use further improvement - read the thread
for further details..

HTH

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew

PaulSchrum - 29 Dec 2005 03:48 GMT
Andrew,

Thanks for your reply.

> Where are you going to put it?  Describe it in words.

It is rather complex to describe in words.  That is why I did not try
to describe it in my original post.  I figured if the post was too long
some people would not read it.

Okay, ready?  Here goes.

It is a type of view onto a document of linguistic markup of text.  I
will give the user the ability to point to a word and tell me that the
word is a noun, verb, adjective, etc.  The user will also tell me which
nouns adjectives modify, which verbs adverbs modify and such.  I will
draw arrows from the modifier to the modifyee.

The page has four columns for simple sentences:  Coordinate
Conjunction, Subject, Verb, Direct Object.  As the user tells me which
words or phrases fall under which category, I tab that text over to the
appropriate column.  If a word starts too far to the right of the page
to be tabbed to the appropriate column, I start a new line and then tab
it over.

Now, all of this can nest.  The above is "simple" in the case of a
sentence with no subordinate clauses and now noun phrases.  A simple
case is "I like programming."  But when there is a subordinate clause
(I like programming because it is fun) not only does the main clause
have a subject, verb, DO, but so does the subordinate clause.  But we
plot that in a smaller frame which is a fraction of the width of the
page.

Have I lost you yet?  Sorry.

As I look over the API documentation I can see that a custom layout
manager is just the thing I need, but now I need to learn how to
implement that.  This brings me back to one of my posts to Tom Hawtin,
where is a good resource for figuring out how to do that?

- Paul
Andrew Thompson - 29 Dec 2005 04:45 GMT
(big snip  ..chuckle, though I asked for it)

> ..This brings me back to one of my posts to Tom Hawtin,
> where is a good resource for figuring out how to do that?

OK, I'd like to clarify this.

Do you know exactly where to draw your strings for
all of what you described?

<personal perspective>
If so, you should be able to transfer that logic (providing
the co-ordinates to place a component) to a LayoutManager
with relative ease - a custom layout is a lot simpler than
it might sound.  If not, no tutorial on writing a
LayoutManager is going to provide that.
</personal perspective>

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew



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.