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 6360877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:42:16+00:00 2026-05-24T23:42:16+00:00

I have class MyPanel that extends from JPanel. MyPanel class has JLabel component which

  • 0

I have class MyPanel that extends from JPanel. MyPanel class has JLabel component which holds an icon.

My question is how can i paint/render this JLabel component to get translucent effect (see through icon) inside MyPanel class (not create xxxJLabel extends JLabel class and override paintComponents method).

Thank you

  • 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-24T23:42:17+00:00Added an answer on May 24, 2026 at 11:42 pm

    One way is to provide a translucent image to the JLabel. That might be done with a standard label, before setIcon() or similar is called, or alternately by extending JLabel and overriding the setIcon() method to do the same.

    E.G. of 2nd technique

    enter image description here

    Code

    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import java.net.URL;
    
    class TransparentIcon {
        public static void main(String[] args) throws Exception {
            String imgURL =
                "http://www.gravatar.com/avatar/" +
                "a1ab0af4997654345d7a949877f8037e";
            final BufferedImage image = ImageIO.read(new URL(imgURL));
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ImageIcon icon = new ImageIcon(image);
    
                    JPanel  p = new JPanel(new GridLayout(2,3));
                    for (int ii=0; ii<6; ii++) {
                        TransparentLabel tl = new TransparentLabel();
                        tl.setOpacity((ii+1)/6f);
                        tl.setIcon(icon);
                        p.add(tl);
                    }
    
                    JOptionPane.showMessageDialog(null, p);
                }
            });
        }
    }
    
    class TransparentLabel extends JLabel {
    
        float opacity = 1f;
    
        public void setOpacity(float opacity) {
            this.opacity = opacity;
        }
    
        private Icon getTranslucentIcon(Icon icon) {
    
            if (icon!=null) {
                BufferedImage bi = new BufferedImage(
                    icon.getIconWidth(),
                    icon.getIconHeight(),
                    BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = bi.createGraphics();
                AlphaComposite ac = AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER,
                    opacity);
                g.setComposite(ac);
                icon.paintIcon(this,g,0,0);
                g.dispose();
    
                return new ImageIcon(bi);
            } else {
                return null;
            }
        }
    
        public void setIcon(Icon icon) {
            super.setIcon( getTranslucentIcon(icon) );
        }
    }
    

    Update

    I just wonder how it can be done if i get Graphics of JLabel inside MyPanel class and change its visual appearance?

    LabelRenderTest.java renders a JLabel to a BufferedImage so that it can be used for custom rendering inside the paintComponent(Graphics) method.

    Note though: I don’t quite understand what the advantage of the JLabel is in your use-case. I was using it in that example in order to render HTML. If I only had an image, I’d use the image directly (e.g. Graphics.drawImage(Image,int,int,ImageObserver)) and never create the label.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that extends JPanel with several buttons on it. I would
I have class method that returns a list of employees that I can iterate
basically, I have a program that has a class to create a basic GUI,
I have two Activities, both have an inner class SurfaceView, which has an inner
I have class(Customer) which holds more than 200 string variables as property. I'm using
I have class that has a property that is of type string[]. I need
I have class (MyCustomWebView) that extend webview can i do something like this ?
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with a member function that takes a default argument. struct Class

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.