I created a JPanel
inside it i put a JCheckBox
and two buttons:
when checkbox is checked a glassPane with transparent color (alpha = 100) is showed above the panel and contains a label displaying the String
"Loading...", the problem is that the location of checkbox changes when glassPane is displayed:
and when i change the size of the frame the glassPane become opaque and all component which were visible under glasspane become invisible.
and when i change the size to smaller size, the background of the glassPane changes and get darker and the label string "Loading" become blurred
Here is my code which is inspired by Oracle tutorial about GlassPane GlassPane Tutorial
package com.training.test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
class Training extends JFrame
{
JCheckBox changeGlass;
JButton button1, button2;
public Training()
{
setBounds(100, 100, 400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Test Glass Pane");
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel glass = (JPanel) getGlassPane();
glass.setOpaque(true);
glass.setBackground(new Color(120, 120, 120, 100));
glass.setLayout(new BorderLayout());
glass.add(new JLabel("Loading ..."));
changeGlass = new JCheckBox("Show Glass Pane", false);
changeGlass.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
glass.setVisible(e.getStateChange() == ItemEvent.SELECTED);
}
});
button1 = new JButton("Select");
button2 = new JButton("Change");
getContentPane().add(changeGlass);
getContentPane().add(button1);
getContentPane().add(button2);
glass.addMouseListener(new MouseListener()
{
public void mouseReleased(MouseEvent e)
{
redispatchEvent(e, (JPanel) getContentPane(), glass);
}
public void mousePressed(MouseEvent e)
{
redispatchEvent(e, (JPanel) getContentPane(), glass);
}
public void mouseExited(MouseEvent e)
{
redispatchEvent(e, (JPanel) getContentPane(), glass);
}
public void mouseEntered(MouseEvent e)
{
redispatchEvent(e, (JPanel) getContentPane(), glass);
}
public void mouseClicked(MouseEvent e)
{
redispatchEvent(e, (JPanel) getContentPane(), glass);
}
});
}
public void redispatchEvent(MouseEvent e, JPanel panel, JPanel glass)
{
Point panelPoint = SwingUtilities.convertPoint(glass, e.getPoint(), panel);
Component c = SwingUtilities.getDeepestComponentAt(panel, (int) panelPoint.getX(), (int) panelPoint.getY());
Point componentPoint = SwingUtilities.convertPoint(panel, panelPoint, c);
if (c != null && c.equals(changeGlass))
{
c.dispatchEvent(new MouseEvent(c, e.getID(), e.getWhen(), e.getModifiers(), (int) componentPoint.getX(), (int) componentPoint.getY(), e.getClickCount(), e
.isPopupTrigger()));
}
else
{
e.consume();
}
}
}
Can anyone correct me what i am doing wrong?
Check out Backgrounds With Transparency for the probable reason for the problem and a couple of solutions. Basically because you are using a transparent background you need to make sure you paint the background of the glass pane yourself.
So I would create a custom panel to use as your glass pane so you can manage the painting of the background. Then you set this panel as the glass pane of the frame.