Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Swing components with special needs?

Reply
Thread Tools

Swing components with special needs?

 
 
Twisted
Guest
Posts: n/a
 
      03-28-2006
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!

 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      03-29-2006
Twisted wrote:
> 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.


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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
 
Twisted
Guest
Posts: n/a
 
      03-29-2006
So it's better to subclass JPanel (or JLayeredPane? Must investigate
that) and, if necessary, put it in a scroll pane as a child?

 
Reply With Quote
 
Monique Y. Mudama
Guest
Posts: n/a
 
      03-29-2006
On 2006-03-29, Thomas Hawtin penned:
> The commonly held belief that JPanel automatically sets opaque is not
> true of all versions of all PL&Fs.


Uhoh! Could you please elaborate?

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      03-29-2006
Monique Y. Mudama wrote:
> On 2006-03-29, Thomas Hawtin penned:
>> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Monique Y. Mudama
Guest
Posts: n/a
 
      03-30-2006
On 2006-03-29, Thomas Hawtin penned:
> Monique Y. Mudama wrote:
>> On 2006-03-29, Thomas Hawtin penned:
>>> 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.
>


I did not know that. Thank you.

--
monique

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swing Model Classes Updating Swing Components on a Thread Other Than AWT mkrause Java 0 05-06-2005 04:32 PM
SWING components adjustment in different resolutions - Should show scrollbars less than 800X600 and expand components over this resolution Bluetears76 Java 1 07-01-2004 09:01 PM
Which swing components to use? Javaman1003 Java 2 02-12-2004 02:01 PM
Can Choice components respond to keyboard input like HTML Choice components? Mickey Segal Java 0 02-02-2004 10:59 PM
Import swing-components in J2ME-Applications Christian Marko Java 2 01-06-2004 05:40 PM



Advertisments