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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:59:05+00:00 2026-05-17T18:59:05+00:00

My issues is that I have created a JPanel that draws a gradient as

  • 0

My issues is that I have created a JPanel that draws a gradient as a background. But when I go to add components to it (like a JButton) it does nothing…

Please help!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class CocoaTrashBar extends JPanel {
    String titleText = "";
    Color topLineColor = new Color(128, 128, 128);
    Color bottomLineColor = new Color(69, 69, 69);
    Color gradientTop = new Color(116, 116, 116);
    Color gradientBottom = new Color(81, 81, 81);


    public CocoaTrashBar() {
        setDefaults();
    }


    public CocoaTrashBar(String title) {
        setDefaults();
        this.titleText = title;
    }


    public void setTitle(String title) {
        this.titleText = title;
        this.repaint();
    }


    private void setDefaults() {
        super.setOpaque(true);
        this.setPreferredSize(new Dimension(0, 24));
        installWindowFocusListener(new WindowFocusListener() {
            public void windowGainedFocus(WindowEvent e) {
                topLineColor = new Color(128, 128, 128);
                bottomLineColor = new Color(69, 69, 69);
                gradientTop = new Color(116, 116, 116);
                gradientBottom = new Color(81, 81, 81);
                repaintComponent();
            }
            public void windowLostFocus(WindowEvent e) {
                topLineColor = new Color(171, 171, 171);
                bottomLineColor = new Color(103, 103, 103);
                gradientTop = new Color(156, 156, 156);
                gradientBottom = new Color(121, 121, 121);
                repaintComponent();
            }
        }, this);
    }


    private static void installWindowFocusListener(
            WindowFocusListener focusListener, JComponent component) {
        component.addPropertyChangeListener("Frame.active",
                createFrameFocusPropertyChangeListener(focusListener, component));
    }


    private static PropertyChangeListener createFrameFocusPropertyChangeListener(
            final WindowFocusListener focusListener, final JComponent component) {
        return new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                Window window = SwingUtilities.getWindowAncestor(component);
                boolean hasFocus = (Boolean) component.getClientProperty("Frame.active");
                if (hasFocus) {
                    focusListener.windowGainedFocus(
                            new WindowEvent(window, WindowEvent.WINDOW_GAINED_FOCUS));
                } else {
                    focusListener.windowLostFocus(
                            new WindowEvent(window, WindowEvent.WINDOW_LOST_FOCUS));
                }
            }
        };
    }


    private void repaintComponent() {
        this.repaint();
    }


    @Override
    public void paint(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d =(Graphics2D)g;
        // Draw first line
        g2d.setPaint(topLineColor);
        g2d.drawLine(0, 0, this.getWidth(), 0);
        // Draw last line
        g2d.setPaint(bottomLineColor);
        g2d.drawLine(0, 23, this.getWidth(), 23);
        // Draw gradient
        GradientPaint gradient = new GradientPaint(
                this.getX(),
                this.getY()+1,
                gradientTop,
                this.getX(),
                this.getHeight()-1,
                gradientBottom);
        g2d.setPaint(gradient);
        g2d.fillRect(this.getX(), this.getY()+1, this.getWidth(), this.getHeight()-2);
        if(titleText != null) {
            g2d.setFont(new Font("", Font.BOLD, 13));
            g2d.setColor(Color.WHITE);
            g2d.drawString(titleText, 10, 16);
        }
        g.dispose();
    }

}
  • 1 1 Answer
  • 1 View
  • 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-17T18:59:05+00:00Added an answer on May 17, 2026 at 6:59 pm

    the problem is with your paint method override calling paintComponent

    replace your paint method with this:

    protected void paintComponent(Graphics g)
    {
        if (isOpaque())
        {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        Graphics2D g2d = (Graphics2D) g;
        // Draw first line
        g2d.setPaint(topLineColor);
        g2d.drawLine(0, 0, this.getWidth(), 0);
        // Draw last line
        g2d.setPaint(bottomLineColor);
        g2d.drawLine(0, 23, this.getWidth(), 23);
        // Draw gradient
        GradientPaint gradient = new GradientPaint(this.getX(), this.getY() + 1, gradientTop, this.getX(), this.getHeight() - 1, gradientBottom);
        g2d.setPaint(gradient);
        g2d.fillRect(this.getX(), this.getY() + 1, this.getWidth(), this.getHeight() - 2);
        if (titleText != null)
        {
            g2d.setFont(new Font("", Font.BOLD, 13));
            g2d.setColor(Color.WHITE);
            g2d.drawString(titleText, 10, 16);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are other questions that have similar issues, but I have read
My issue is that I have created a table for a local db in
Is there any way to do it? I often have issues that work locally
I have an other active question HERE regarding some hopeless memory issues that possibly
I've just started using NHibernate, and I have some issues that I'm unsure how
I have an express server that has a button that issues a POST which
I have a couple of things I'm working on, namely a page that issues
I have some issues with a PHP file that is not working properly. The
I have created a simple API for a Rails application using token-based-authentication that supports
I have created the output for a program that allows a user to input

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.