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

  • Home
  • SEARCH
  • 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 577681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:10:46+00:00 2026-05-13T14:10:46+00:00

I’m trying to create a translucent window with Java on OSX and add a

  • 0

I’m trying to create a translucent window with Java on OSX and add a JLabel to it.

This JLabel changes its text every second….

alt text

However the component is not repainting well.

How can I solve this problem?

I’ve found the these articles, but I can’t figure out how to solve it.

If possible, please paste the fixing source code, here’s mine:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.util.Timer;
import java.util.TimerTask;

public class Translucent {
    public static void main( String [] args ) {

        JFrame frame = new JFrame();

        frame.setBackground( new Color( 0.0f,0.0f,0.0f,0.3f));

        final JLabel label =  new JLabel("Hola");
        label.setFont( new Font( label.getFont().getFamily(), Font.PLAIN, 46 ) );
        label.setForeground( Color.white );

        frame.add( label );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

        Timer timer = new Timer();
        timer.schedule( new TimerTask(){
            int i = 0;
            public void run() {
                label.setText("Hola "+ i++ );
            }
        }, 0, 1000 );


    }   
}
  • 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-13T14:10:46+00:00Added an answer on May 13, 2026 at 2:10 pm

    I’ve had some luck extending JLabel and implementing Icon to get a translucent component working the way I want. You can see the result of various rule combinations in this AlphaCompositeDemo. The example below is 100% white atop 50% black.

    Addendum: Note how this example composites opaque text on a clear offscreen background over the translucent frame background.

    Addendum: Here’s a way to make the whole frame translucent. Unfortunately, it dims the content, too.

    image

    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Translucent extends JPanel implements ActionListener {
    
        private static final int W = 300;
        private static final int H = 100;
        private static final Font font =
            new Font("Serif", Font.PLAIN, 48);
        private static final SimpleDateFormat df =
            new SimpleDateFormat("HH:mm:ss");
        private final Date now = new Date();
        private final Timer timer = new Timer(1000, this);
        private BufferedImage time;
        private Graphics2D timeG;
    
        public Translucent() {
            super(true);
            this.setPreferredSize(new Dimension(W, H));
            timer.start();
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = this.getWidth();
            int h = this.getHeight();
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(0, 0, w, h);
            g2d.setComposite(AlphaComposite.Src);
            g2d.setPaint(g2d.getBackground());
            g2d.fillRect(0, 0, w, h);
            renderTime(g2d);
            int w2 = time.getWidth() / 2;
            int h2 = time.getHeight() / 2;
            g2d.setComposite(AlphaComposite.SrcOver);
            g2d.drawImage(time, w / 2 - w2, h / 2 - h2, null);
        }
    
        private void renderTime(Graphics2D g2d) {
            g2d.setFont(font);
            String s = df.format(now);
            FontMetrics fm = g2d.getFontMetrics();
            int w = fm.stringWidth(s);
            int h = fm.getHeight();
            if (time == null && timeG == null) {
                time = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                timeG = time.createGraphics();
                timeG.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
                timeG.setFont(font);
            }
            timeG.setComposite(AlphaComposite.Clear);
            timeG.fillRect(0, 0, w, h);
            timeG.setComposite(AlphaComposite.Src);
            timeG.setPaint(Color.green);
            timeG.drawString(s, 0, fm.getAscent());
        }
    
        private static void create() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setBackground(new Color(0f, 0f, 0f, 0.3f));
            f.setUndecorated(true);
            f.add(new Translucent());
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            now.setTime(System.currentTimeMillis());
            this.repaint();
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    create();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use the server cache: public ActionResult Index() {… May 14, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer Make it a global: Smalltalk at: #other put: Other new May 14, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer Yes, you should release your view controller after passing it… May 14, 2026 at 10:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.