I have a JFrame
and I want to set it disabled on some loading processes. For that purpose I've created the DisablingLayeredPane
class:
public class DisablingLayeredPane extends JPanel {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f));
g2d.setColor(Color.BLACK); // With 0.2f alpha it looks like light gray
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
}
On the loading process start, I call
frame.getLayeredPane().add(darkeningPane, JLayeredPane.MODAL_LAYER);
But the problem is, this darkening pane does not catch any events, I still can press buttons on my frame. I've also tried AWTEventListener
to consume all of events for this frame, but there is another issue: sometimes I need to show modal dialog for some confirmation, and events for modal dialog are consumed too (I can't press any button). Of course, I can use some tricks like using transparent window
above my frame instead darkening pane
, or use a lot of if-else
statements at AWTEventListener
, but I'm looking for a some beautiful solution, if any.
Thanks in advance.
UPD:
I've also tried to add Mouse and Key Listeners
to layered panel, but there is a new issue: if use JDialog
instead JFrame
, the KeyListener
of layered panel would not catch ESC
key pressing and dialog would dispose.
UPD2:
I've tried frame.setGlassPane(darkeningPane)
instead setting as layer, but there is no effect.