Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Having a JPanel 'float' semi-transparently over another component

843804Jan 26 2005 — edited Jan 27 2005
I am a programmer of a java project for our company.

Managemnt decided that when a certain event happens, we need to 'semi-disable' a certain text area (in a JScrollPane), and have a floating message with a progress bar on top of this text area, but be semi-transparent, so you can still read the text under it.

(Basically, they want it to look like a html page with a floating, semi-transparent DIV, because that is how another group mocked it up).


I am trying to implement this, but am running into problems.
Here is what I have, below I'll tell you what is wrong with it.
/**
     * The purpose of this class is to have a scroll pane that can have it's contents partially covered by another panel
     * while still being able to read both the original panel and the new covering content, and still being able to scroll
     *the content under the covering panel.
     */
    public class JOverlayScrollPane extends JScrollPane{
        
        private JPanel overlay = null;
        private Insets overlayInsets = null;
        private java.awt.AlphaComposite blend = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f); 
        private ComponentAdapter cl = null;
        
        public void setOverlay(JPanel pan, Insets inset){
            overlay = pan;
            overlayInsets = inset;
            
            if(cl != null){
                cl = new ComponentAdapter(){
                    public void componentResized(ComponentEvent e){
                        resizeOverlay();
                    }
                };
            }
                
            resizeOverlay();
                
            repaint();
        }
        
        public void paint(Graphics g){
            super.paint(g);
            
            if(g instanceof Graphics2D && overlay !=null){
                Graphics2D g2 = (Graphics2D)g;
                
//                g2.setComposite(blend);
                
                //overlay.paint(g2);
                paintStuff(g,overlay);
            }
        }
        
        private void resizeOverlay(){
            if(overlay != null){
                Dimension size = getSize();
                int x = 0;
                int y = 0;
                if(overlayInsets !=null){
                    x = overlayInsets.left;
                    y = overlayInsets.top;
                    size.width = size.width - overlayInsets.left - overlayInsets.right;
                    size.height = size.height - overlayInsets.top - overlayInsets.bottom;
                }
                overlay.reshape(x,y, size.width, size.height);
                overlay.doLayout();
                overlay.validate();
            }
        }
        
        private void paintStuff(Graphics g,Component c){
            if(c != null){
                c.paint(g);
                if(c instanceof Container){
                    Container cont = (Container)c;
                    for(int i=0;i<cont.getComponentCount();i++){
                        Component cc = cont.getComponent(i);
                        paintStuff(g,cc);
                    }
                }
            }
        }
    }//end of overlay scroll pane
(I am having problems, so for now, the alpha blend is commented out).

The first version didn't have the paintStuff() method (it just called paint). This just drew a big grey box, now of the sub-components of the passed in JPanel were drawing. I added the do layout and validate calls, without success.

Then I added the paintStuff call, and all the subcomponents now, draw, but they all draw at 0,0.

Questions
1. Is the the correct approach to do this, or sould I be playing with the glass pane or some other approach?
2. It seems that the overlay panel isn't being layed out / doens't paint it's children correctly. Is this because it isn't really part of the layout (i.e. it has no parent / is never added to a container) or am I just missing a step in 'faking' adding it to a layout?
3. I'm sure I could just override paint and paint my own stuff (hand draw the text and a progress bar), but I would really like to put everything on one JPanel as what we want to display my be different in the future. I know that I manually ahve to call repaint on this scrollpane if one of the components on the overlay JPanel change in appearence (the progress bar especailly), and that they won't get events (I don't care about this as they are all non-interactive for now). Is this a viable approach, or is there a better way to do this?

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2005
Added on Jan 26 2005
7 comments
1,275 views