Using GlassPane to block input in JFrame

Using GlassPane to block input in JFrame
Since time, I have been stumped of how to use glass pane even when seeing the tutorial. Now, here is an example on how to use GlassPane to block input in JFrame by creating a transparent black background for that GlassPane with a busy symbol as if some data is being processed. When the user clicks any where on the JFrame when processing the data then a beep sound is played.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BlockInput extends JFrame
JButton b;

public BlockInput()
private void createAndShowGUI()
setTitle("Block input");
setLayout(new FlowLayout());
// Create JButton
b=new JButton("Block input");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
// Create a JPanel with semi-transparent
// black background
// This will be glass pane
JPanel p=new JPanel(){
public void paintComponent(Graphics g)
g.setColor(new Color(0,0,0,140));
// Set it non-opaque
// Set layout to JPanel
p.setLayout(new GridBagLayout());
// Add the jlabel with the image icon
p.add(new JLabel(new ImageIcon("loading.gif")));
// Take glass pane
// Add MouseListener
p.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me)
// Consume the event, now the input is blocked
// Create beep sound, when mouse is pressed
// Make it visible, it isn't by default because
// it is set as glass pane
public static void main(String args[])
new BlockInput();

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)

“Genius is 1% inspiration 99% perspiration” - Thomas Alva Edison

Posted by at on

Tags: Swing Components, Swing Event Handling,