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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:54:26+00:00 2026-06-11T18:54:26+00:00

I know that I can fade in a panel, by adding the alpha value

  • 0

I know that I can fade in a panel, by adding the alpha value to the background color & a timer. But how can I fade in a panel with child components (like a JLabel)?

EDIT

_fadeTimer = new Timer(40, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (_alpha == 255) {
                _fadeTimer.stop();
            } else {
                pnl_hint.setBackground(new Color(
                        bgColor.getRed(),
                        bgColor.getGreen(),
                        bgColor.getBlue(),
                        (_alpha += 1)));

                pnl_hint.updateUI();
            }
        }
    });
    _fadeTimer.start();
  • 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-06-11T18:54:28+00:00Added an answer on June 11, 2026 at 6:54 pm

    Another option would be to capture a screenshot of your panel and then let it paint itself with an increasing alpha composite.

    Here is a small demo of that (although I am not sure this is really clean):

    import java.awt.AlphaComposite;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class TestFading {
    
        private static class FadingPanel extends JPanel {
            private BufferedImage buffer;
            private boolean isFading = false;
            private long start;
            private float alpha = 1.0f;
    
            @Override
            public void paint(Graphics g) {
                if (isFading) {// During fading, we prevent child components from being painted
                    g.clearRect(0, 0, getWidth(), getHeight());
                    ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                    g.drawImage(buffer, 0, 0, this);// We only draw an image of them with an alpha
                } else {
                    super.paint(g);
                }
            }
    
            public void fade(int time) {
                start = System.currentTimeMillis();
                buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
                this.print(buffer.getGraphics());// Draw the current components on the buffer
                isFading = true;
                final int timeInMillis = time * 1000;
                final Timer t = new Timer(50, null);
                t.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        long elapsed = System.currentTimeMillis() - start;
                        if (elapsed > timeInMillis) {
                            start = 0;
                            isFading = false;
                            buffer = null;
                            repaint();
                            t.stop();
                        } else {
                            alpha = 1.0f - (float) elapsed / timeInMillis;
                            repaint();
                        }
                    }
                });
                t.start();
            }
        }
    
        protected void initUI() throws MalformedURLException {
            JFrame frame = new JFrame(TestFading.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final FadingPanel panel = new FadingPanel();
            JTextField textfield = new JTextField(60);
            JLabel image = new JLabel(new ImageIcon(new URL("http://helios.gsfc.nasa.gov/image_mag_stamp.jpg")));
            JButton button = new JButton("Fade");
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    // I use an invoke later to allow the button to release itself
                    SwingUtilities.invokeLater(new Runnable() {
    
                        @Override
                        public void run() {
                            panel.fade(5);// Fade the panel in 5s.
                        }
                    });
                }
            });
            panel.add(textfield);
            panel.add(image);
            panel.add(button);
            frame.add(panel);
            frame.setSize(400, 300);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        new TestFading().initUI();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that can't use left join in indexed view. but I don't understand
I know that it's a subject that can raise a lot of debate, but
I know that I can get the true bits with the simple loop, but
I know you can fade out a <div> with jQuery, but I was wondering
I want to fade a WPF window in, but I can't do that unless
How can I give a tableView cell background color the fade effect as soon
I know that can co-exists with web form no problem as Hanselman mention here
I just want to know that can i make an application which have some
I want to know that can mouse click event detects colors or even strings.
I know that I can hijack a form by showing a login form in

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.