6

I am able to make JFrame totally transparent and the JButton is partly transparent just fine until I move my mouse on the button ( do not click ) and move the mouse off from the button ( MouseExited called via MouseListener ). What happens is that the background of the JButton is drawn again, so after couple of mouse movements on and off the button the button is totally opaque.

public class ButtonExample extends JWindow
{
   public ButtonExample( )
   {
        JButton But = new JButton( "Testing" );
        But.setBackground( new Color( 0, 0, 0, 200 ) );
        But.setForeground( new Color( 70, 155, 255 ) );
        this.add( But );
        this.setBackground( new Color( 0, 0, 0, 0 ) );
        this.setMinimumSize( new Dimension( 200,100 ) );
        this.setVisible( true );
    }

    public static void main( String[ ] Args ) 
    {
        new ButtonExample( );
    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Pete
  • 63
  • 1
  • 2
  • 4

2 Answers2

9

problem is that the button reports being fully opaque when in fact it isn't (due to the partially transparent color)

  but.setOpaque(false);

BTW: as you see I changed the field name to conform to java naming conventions :-)

Edit

arggghh .. missed that, sorry. Need to check what we do in SwingX, from the top of my head I would say you need to override paintComponent and handle the background painting yourself, like

        /** 
         * @inherited <p>
         */
        @Override
        protected void paintComponent(Graphics g) {
            if (!isOpaque() && getBackground().getAlpha() < 255) {
                g.setColor(getBackground());
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            super.paintComponent(g);
        }

didn't try, though, maybe the "getting more opaque" is back again with doing so .. will come back tomorrow

Edit 2

okay, checked - the edited code works correctly. So in summary: components with translucent background

  • must report that they are not opaque to not confuse the default painting mechanism
  • must take over the background painting and fill it with the background color themselves (SwingX JXPanel f.i. does by explicit support for an alpha property)

for your convenience, here's a small runnable with incorrect/correct background side-by-side

public class TransparentButton  {

    public TransparentButton() {
        JWindow incorrectOpaque = createWindow("incorrect opaque", true);
        incorrectOpaque.setLocation(600, 600);
        incorrectOpaque.setVisible(true);
        JWindow correctOpaque = createWindow("correct opaque", false);
        correctOpaque.setLocation(800, 600);
        correctOpaque.setVisible(true);
    }


    private JButton createButton(final boolean opaque) {
        JButton but = new JButton("Testing") {

            /**
             * @inherited <p>
             * Overridden to take over background painting with 
             * transparent color.
             */
            @Override
            protected void paintComponent(Graphics g) {
                if (!isOpaque() && getBackground().getAlpha() < 255) {
                    g.setColor(getBackground());
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                super.paintComponent(g);
            }

        };
        but.setBackground(new Color(0, 0, 0, 100));
        but.setForeground(new Color(70, 155, 255));
        but.setOpaque(opaque);
        return but;
    }

    private JWindow createWindow(String text, boolean opaque) {
        JWindow window = new JWindow();
        JButton but = createButton(opaque);
        window.add(but);
        window.add(new JLabel(""), BorderLayout.SOUTH);
        window.setOpacity(0.5f);
        window.setBackground(new Color(0, 0, 0, 0));
        window.setSize(new Dimension(200, 100));
        return window;
    }

    public static void main(String[] Args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                new TransparentButton();
            }
        });
    }

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger.getLogger(TransparentButton.class
            .getName());
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • By setting opaque false the button background gets totally transparent( not painted ) which is not what I want at all. Can you assist me further to get the background to show when opaque is false? – Pete Sep 14 '11 at 17:50
0

Have you tried translucency?

The translucent and shaped window API was first added to the Java SE 6 Update 10 release as a private API. This functionality was moved to the public AWT package in the JDK 7 release.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • There is no problem in making the window transparent. The fix would how probably include modifying the painting of the component ( JButton in this case ) itself and is not related to the window straight. – Pete Sep 10 '11 at 18:35