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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:02:26+00:00 2026-05-24T01:02:26+00:00

How can I make the JButton visible? 1) When no progressive background is turned

  • 0

How can I make the JButton visible?

1) When no progressive background is turned on: JButton is showing

enter image description here

2) When no progressive background is turned on, JButton is pressed still showing no flicker:

enter image description here

3) When progressive background is turned on, JButton is invisible and on pressing in this I see flicker and JButton() appears and again hides auto. << Problem is here, so how can I fix it?

enter image description here

import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.*;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class ButtonTest extends JWindow implements MouseListener, MouseMotionListener {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private SoftJButton softButton1;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ButtonTest j = new ButtonTest();
                j.createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        softButton1 = new SoftJButton("Transparent Button");
        softButton1.setBackground(Color.GREEN);
        softButton1.setAlpha(0.5f);
        softButton1.setDoubleBuffered(true);
        this.setLayout(new BorderLayout());
        this.setBounds(100, 30, 200, 100);
        this.setBackground(new Color(0, 0, 0, 255));
        this.setVisible(true);
        add(softButton1);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public static class SoftJButton extends JButton {

        private static final JButton lafDeterminer = new JButton();
        private static final long serialVersionUID = 1L;
        private boolean rectangularLAF;
        private float alpha = 1f;

        public SoftJButton() {
            this(null, null);
        }

        public SoftJButton(String text) {
            this(text, null);
        }

        public SoftJButton(String text, Icon icon) {
            super(text, icon);
            setOpaque(false);
            setFocusPainted(false);
        }

        public float getAlpha() {
            return alpha;
        }

        public void setAlpha(float alpha) {
            this.alpha = alpha;
            repaint();
        }

        @Override
        public void paintComponent(java.awt.Graphics g) {
            java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            if (rectangularLAF && isBackgroundSet()) {
                Color c = getBackground();
                g2.setColor(c);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            super.paintComponent(g2);
        }

        @Override
        public void updateUI() {
            super.updateUI();
            lafDeterminer.updateUI();
            rectangularLAF = lafDeterminer.isOpaque();
        }
    }
}
  • 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-24T01:02:27+00:00Added an answer on May 24, 2026 at 1:02 am

    It’s not clear how your video source works, but it appears to be incompatible with Swing due to Mixing Heavyweight and Lightweight Components. Although awt components aren’t transparent, you can always draw your own translucent text on the Frame and do manual hit testing, as suggested below. You might also check to see if your video source API supports Double Buffering and Page Flipping.

    enter image description here

    import java.awt.Color;
    import java.awt.AlphaComposite;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.Rectangle2D;
    
    /** @see http://stackoverflow.com/questions/6725618 */
    public class AlphaFrame extends Frame {
    
        private static final Font font = new Font("Dialog", Font.BOLD, 24);
        private float alpha = 1f;
        private Rectangle rect = new Rectangle();
    
        public static void main(String[] args) {
            AlphaFrame af = new AlphaFrame();
            af.setTitle("Translucent Button");
            af.setAlpha(0.5f);
            af.setForeground(Color.green);
            af.setBackground(Color.black);
            af.setVisible(true);
        }
    
        public AlphaFrame() {
            this.setSize(320, 240);
            this.setLocationRelativeTo(null);
            this.setBackground(Color.white);
            this.addWindowListener(new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            this.addMouseListener(new MouseAdapter() {
    
                @Override
                public void mousePressed(MouseEvent e) {
                    if (rect.contains(e.getPoint())) {
                        System.out.println("Pressed.");
                    }
                }
            });
            repaint();
        }
    
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setComposite(AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, alpha));
            g2.setColor(getForeground());
            g2.setFont(font);
            FontMetrics fm = g2.getFontMetrics();
            String s = getTitle();
            Insets i = getInsets();
            int dx = i.left + 10;
            int dy = i.top + fm.getHeight() + 10;
            Rectangle2D bounds = fm.getStringBounds(s, g);
            rect.x = dx + (int) bounds.getX() - 1;
            rect.y = dy + (int) bounds.getY() - 1;
            rect.width = (int) bounds.getWidth() + 1;
            rect.height = (int) bounds.getHeight() + 1;
            System.out.println(i + " \n" + bounds + "\n" + rect);
            g2.drawRect(rect.x, rect.y, rect.width, rect.height);
            g2.drawString(s, dx, dy);
        }
    
        public float getAlpha() {
            return alpha;
        }
    
        public void setAlpha(float alpha) {
            this.alpha = alpha;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i make AWT component transparent when the background is progressive image? Note:
I know that I can make a setter that checks to see if a
I can make a obj to use the canvas to draw like this: MyObj.myDiv
I hope I can make this question clear enough. I'm looking to put a
How can i completely make this grey panel as transparent , so that i
I can make the text fit inside the div? <div> Hello! This a long
How can I make an array of image icons? When I click a JButton
I can make a DAO recordset in VB6/Access do anything - add data, clean
I can make Firefox not display the ugly dotted focus outlines on links with
How can make a link within a facebox window that redirects it to another

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.