Swing components with special needs?

Discussion in 'Java' started by Twisted, Mar 28, 2006.

  1. Twisted

    Twisted Guest

    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!
     
    Twisted, Mar 28, 2006
    #1
    1. Advertising

  2. 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/
     
    Thomas Hawtin, Mar 29, 2006
    #2
    1. Advertising

  3. Twisted

    Twisted Guest

    So it's better to subclass JPanel (or JLayeredPane? Must investigate
    that) and, if necessary, put it in a scroll pane as a child?
     
    Twisted, Mar 29, 2006
    #3
  4. 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
     
    Monique Y. Mudama, Mar 29, 2006
    #4
  5. 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/
     
    Thomas Hawtin, Mar 29, 2006
    #5
  6. 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
     
    Monique Y. Mudama, Mar 30, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Marko
    Replies:
    2
    Views:
    3,959
    Tim Tyler
    Jan 6, 2004
  2. Mickey Segal
    Replies:
    0
    Views:
    941
    Mickey Segal
    Feb 2, 2004
  3. Javaman1003

    Which swing components to use?

    Javaman1003, Feb 12, 2004, in forum: Java
    Replies:
    2
    Views:
    411
  4. T E Schmitz
    Replies:
    3
    Views:
    434
  5. mkrause
    Replies:
    0
    Views:
    700
    mkrause
    May 6, 2005
Loading...

Share This Page