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 / General / March 2006

Tip: Looking for answers? Try searching our database.

Swing components with special needs?

Thread view: 
Twisted - 28 Mar 2006 22:24 GMT
Suppose I want to make a Swing component that contains objects that
need to show selection effects and control handles (e.g. resize or
rotate handles)? Or one that contains a child component, which paints
itself somehow, and needs to show an overlaying grid with one square
selected? How would this best be accomplished?

I can think of a couple alternatives:

* Override paint. Put paintComponent after paintChildren, and draw
selection related stuff in paintComponent; handles etc. appear in front
of children.
* Selection stuff gets added as children, which may present a
management headache but keeps paint from needing to be overridden.
* Something I haven't thought of yet.

In the first case, would one's paint override just be like this?:

public void paint (Graphics g) {
       paintChildren(g);
       paintComponent(g);
       paintBorder(g);
}

or are there other things that should be done inside paint besides
these three calls? (And should they get a copy of the Graphics object,
rather than the same one passed to paint?)

The ideal thing would really be to have two layers, one for the
selection related stuff in front of one for the "real" children.

Also, should such a component subclass JComponent directly, or JPanel
or even JScrollPane or similar?

What do the experienced Swing programmers among you say?

--
I am the terror that flaps in the net!
I am the obscure crash that happens in a JNI call and leaves you no
debugging info!
I am TWISTED!
Thomas Hawtin - 29 Mar 2006 07:28 GMT
> Suppose I want to make a Swing component that contains objects that
> need to show selection effects and control handles (e.g. resize or
[quoted text clipped - 7 lines]
> selection related stuff in paintComponent; handles etc. appear in front
> of children.

You could do something like that. IIRC, you'll need to switch optimised
drawing off.

> * Selection stuff gets added as children, which may present a
> management headache but keeps paint from needing to be overridden.

Headache is the important word in that sentence.

> * Something I haven't thought of yet.

My initial reaction is to go for a JLayeredPane.

> Also, should such a component subclass JComponent directly, or JPanel
> or even JScrollPane or similar?

If you have child components that appear to the user as components, use
JPanel as accessibility will be correct. The commonly held belief that
JPanel automatically sets opaque is not true of all versions of all
PL&Fs. JScrollPane isn't going to be easy to extend in a meaningful manner.

Tom Hawtin
Signature

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

Twisted - 29 Mar 2006 18:10 GMT
So it's better to subclass JPanel (or JLayeredPane? Must investigate
that) and, if necessary, put it in a scroll pane as a child?
Monique Y. Mudama - 29 Mar 2006 19:30 GMT
> The commonly held belief that JPanel automatically sets opaque is not
> true of all versions of all PL&Fs.

Uhoh!  Could you please elaborate?

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Thomas Hawtin - 29 Mar 2006 20:44 GMT
>> The commonly held belief that JPanel automatically sets opaque is not
>> true of all versions of all PL&Fs.
>
> Uhoh!  Could you please elaborate?

The relevant setOpaque call comes from the PL&F, and is not set from
within the JPanel itself. IIRC, GTK on 1.5 is an example of a PL&F that
sets the opaque property to false. But not on 1.6.

When you set a JPanel as the content pane of a JFrame, technically you
should call setOpaque on it to avoid potential performance problems.

Tom Hawtin
Signature

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

Monique Y. Mudama - 30 Mar 2006 20:15 GMT
>>> The commonly held belief that JPanel automatically sets opaque is
>>> not true of all versions of all PL&Fs.
[quoted text clipped - 8 lines]
> you should call setOpaque on it to avoid potential performance
> problems.

I did not know that.  Thank you.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html



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.