Here's what I'm trying to do:
When the user pauses my application I want to grey-out the data area and display the word "Paused..." over it. Ideally, the user will still be able to see the data and scroll through it.
What I've tried:
This seems to have me about 75% there. I extended JLayeredPane to OverlayPane. What this does is add a JPanel that stays on top of everything else that gets added to the container and is the same size as the container. I have overridden the: add, remove, removeAll, moveToFront, moveToBack, setBounds and setLocation methods for this purpose.
What's happening:
When I click Pause the overlay becomes visible the word "Paused..." appears and I can see the data through the panel. However, when I scroll the data the panel does not get redrawn, just the JScrollPane the data resides in. I think I need to add some listeners to trigger a redraw of the overlay if it is set to visible, but I'm not sure what listener or where to add it.
edit by helloworld922: In the future please post your code in the post, particularly when it's not too long.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.hammers.itos.schedule_executor; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Point; import java.awt.Rectangle; import javax.swing.JLayeredPane; import javax.swing.JPanel; /** * * @author Sean Smitz */ public class OverlayPane extends JLayeredPane{ private JPanel overlay; public OverlayPane(){ super(); overlay = new JPanel(); overlay.setBounds(getBounds()); overlay.setBackground(new Color(51, 51, 51, 128)); overlay.setForeground(Color.WHITE); add(overlay); moveToFront(overlay); } public Color getOverlayBackgroundColor(){ return overlay.getBackground(); } public Color getOverlayForegroundColor(){ return overlay.getForeground(); } public void setOverlayBackgroundColor(Color color){ overlay.setBackground(color); } public void setOverlayForegroundColor(Color color){ overlay.setForeground(color); } public Component addComponentToOverlay(Component comp){ return overlay.add(comp); } public void removeComponentFromOverlay(Component comp){ overlay.remove(comp); } public void removeAllComponentsFromOverlay(){ overlay.removeAll(); } public boolean isOverlayVisible(){ return overlay.isVisible(); } public void setOverlayVisible(boolean aFlag){ overlay.setVisible(aFlag); } @Override public Component add(Component comp){ Component ret = null; ret = super.add(comp); moveToFront(overlay); return ret; } @Override public void remove(int index){ if(index != this.getIndexOf(overlay)) super.remove(index); } @Override public void removeAll(){ for(int i = 0; i < this.getComponentCount(); i++){ remove(i); } } @Override public void moveToFront(Component comp){ super.moveToFront(comp); super.moveToFront(overlay); } @Override public void moveToBack(Component comp){ super.moveToBack(comp); super.moveToFront(overlay); } @Override public void setBounds(Rectangle r){ setBounds(r.x, r.y, r.width, r.height); } @Override public void setBounds(int x, int y, int width, int height){ super.setBounds(x, y, width, height); overlay.setBounds(x, y, width, height); } @Override public void setLocation(Point p){ setLocation(p.x, p.y); } @Override public void setLocation(int x, int y){ super.setLocation(x, y); overlay.setLocation(x, y); } @Override public void setMaximumSize(Dimension maximumSize) { super.setMaximumSize(maximumSize); overlay.setMaximumSize(maximumSize); } @Override public void setMinimumSize(Dimension minimumSize) { super.setMinimumSize(minimumSize); overlay.setMinimumSize(minimumSize); } @Override public void setPreferredSize(Dimension preferredSize) { super.setPreferredSize(preferredSize); overlay.setPreferredSize(preferredSize); } @Override public void setSize(Dimension d) { setSize(d.width, d.height); } @Override public void setSize(int width, int height) { super.setSize(width, height); overlay.setSize(width, height); } }