Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6553835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:37:43+00:00 2026-05-25T12:37:43+00:00

I am able to make JFrame totally transparent and the JButton is partly transparent

  • 0

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( );
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:37:43+00:00Added an answer on May 25, 2026 at 12:37 pm

    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());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not able to make a clear decision on this one. I am
I am not able to make this out: Between eliminates use of >= and
I'm not being able to make this line work with Tk import os while(1):
I've been able to make a query using MS Access 2010 that does just
I am not able to make the upper view scroll with listview. The top
I am not able to make more than one request at a time in
My ultimate goal is to be able to make use of additional mouse buttons
I am not able to make the overflow hidden(horizontally) take effect when I apply
I want to be able to make an HTTP call updating some select boxes
I need to be able to make a program that looks through a mailbox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.