001
002package jmri.util.swing;
003
004import java.awt.Component;
005
006import javax.swing.JComponent;
007import javax.swing.JPanel;
008
009/**
010 * Some utilities to turn a recursively component background transparent
011 * 
012 * @author Lionel Jeanson 2025
013 */
014public class TransparencyUtils {
015
016    public static void setTransparent(JComponent jcomp) {
017        setTransparent(jcomp, true);
018    }
019
020    public static void setTransparent(JComponent jcomp, boolean transparency) {
021        if (jcomp instanceof JPanel) { //OS X: Jpanel components are enough
022            jcomp.setOpaque(!transparency);
023        }
024        setTransparent(jcomp.getComponents(), transparency);
025    }
026
027    private static void setTransparent(Component[] comps, boolean transparency) {
028        for (Component comp : comps) {
029            try {
030                if (comp instanceof JComponent) {
031                    setTransparent((JComponent) comp, transparency);
032                }
033            } catch (Exception e) {
034                // Do nothing, just go on
035            }
036        }
037    }
038    
039}