2

I created a JPanel inside it i put a JCheckBox and two buttons:

enter image description here

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:

enter image description here

and when i change the size of the frame the glassPane become opaque and all component which were visible under glasspane become invisible.

enter image description here

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

enter image description here

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?

Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
  • Swing does not do semi-transparent well, I'm afraid. There are tricks to this, but I'll have to look them up. – Hovercraft Full Of Eels Oct 18 '14 at 17:10
  • @HovercraftFullOfEels So transparence with glassPane is not possible like it works with setOpacity of JFrame, i found a solution is to prepare png picture with lower alpha and add the picture to label as imageIcon, but i don't know how to stretch the picture to take all glassPane space. – Naruto Biju Mode Oct 18 '14 at 17:37
  • 1
    I believe that it's possible, but it takes some tricks (where are @camickr or MadProgrammer when you need them?). Perhaps it would be better to use a JLayer. I'm checking that out right now. – Hovercraft Full Of Eels Oct 18 '14 at 17:38

1 Answers1

4

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.

camickr
  • 321,443
  • 19
  • 166
  • 288