2

Is there any way I can make translucent JInternalFrame uing swing?

All I found is option for JFrame(not internal one) which is not what I need.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
maxpovver
  • 1,580
  • 14
  • 25

1 Answers1

2

You should be able to use a JLayer. You could start with the layout painting a fully opaque layer the same color as the background of the desktop pane. Then you change the alpha value to approach cell until you have full transparency.

See the section from the Swing tutorial on How to Decorate Components With the JLayer Class for more information and an example of painting with transparency.

If you only care about the background you can use the Alpha Container when using components with transparency:

JPanel content = new JPanel( new BorderLayout() );
content.setBackground( new Color(0, 0, 0, 0) );
internalFrame.setContentPane(new AlphaContainer(content));
internalFrame.setOpaque(false);

You can then change the alpha value of the content panel to your desired transparency.

Edit:

Here is may attempt at animating the transparency of an internal frame and its components:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class TransparentInternalFrame extends JInternalFrame implements ActionListener
{
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;
    private float alpha = 0.0f;
    private Timer timer = new Timer(500, this);

    public TransparentInternalFrame()
    {
        super("Document #" + (++openFrameCount), true, true, true, true);
        setSize(300,300);
        setLocation(xOffset * openFrameCount, yOffset * openFrameCount);
        setVisible( true );
    }

    @Override
    public void paint(Graphics g)
    {
        g.setColor( getDesktopPane().getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());

        Graphics2D g2 = (Graphics2D)g.create();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
        super.paint(g2);
    }

    public void showFrame()
    {
        timer.start();
    }

    public void hideFrame()
    {
        alpha = 0.0f;
        repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        alpha += .05f;
        alpha = Math.min(1.0f, alpha);
        System.out.println(alpha);

        if (alpha >= 1.0f)
            timer.stop();

        repaint();
    }

    private static void createAndShowGUI()
    {
        JDesktopPane desktop = new JDesktopPane();
        desktop.setBackground( Color.YELLOW );

        TransparentInternalFrame tif = new TransparentInternalFrame();
        desktop.add( tif );
        tif.add(new JButton("Hello"), BorderLayout.PAGE_START);

        JButton show = new JButton( "Show Internal Frame" );
        show.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                tif.showFrame();
            }
        });

        JButton hide = new JButton( "Hide Internal Frame" );
        hide.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                tif.hideFrame();
            }
        });

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(show, BorderLayout.PAGE_START);
        frame.add(desktop);
        frame.add(hide, BorderLayout.PAGE_END);
        frame.setLocationByPlatform( true );
        frame.setSize(500, 500);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • can't find anything about using JLayer for real components, not some demo panels. can you give an example of correctly overriden paint method for LayerUI that enables translucency for layer's component's child objects? – maxpovver Jul 17 '15 at 15:50
  • @maxpovver, It doesn't make all the components translucent. As I suggested it would just make the top layer slowly translucent so it might appear the each component is slowly becoming more opaque. That is the best I can suggest. – camickr Jul 17 '15 at 18:40