Monday, 11 November 2013

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()


{


createAndShowGUI();


}




private void createAndShowGUI()


{


setTitle("Block input");


setLayout(new FlowLayout());


setDefaultCloseOperation(EXIT_ON_CLOSE);




// Create JButton


b=new JButton("Block input");


add(b);




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));


g.fillRect(0,0,getWidth(),getHeight());


}


};




// Set it non-opaque


p.setOpaque(false);




// 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


setGlassPane(p);




// Add MouseListener


p.addMouseListener(new MouseAdapter(){


public void mousePressed(MouseEvent me)


{


// Consume the event, now the input is blocked


me.consume();




// Create beep sound, when mouse is pressed


Toolkit.getDefaultToolkit().beep();


}


});




// Make it visible, it isn't by default because


// it is set as glass pane


p.setVisible(true);


}


});




setSize(400,400);


setVisible(true);


setLocationRelativeTo(null);


}




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

No comments:

Post a Comment