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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:41:35+00:00 2026-05-21T23:41:35+00:00

I have a view object that is a jPanel and holds other jPanels which

  • 0

I have a view object that is a jPanel and holds other jPanels which in turn hold jLabels. I’m wanting to paint a gradient overlay on the object to give it a nice sleek look rather than the boring plain look.

My attempt thus far is:

public class InfoDisplay extends javax.swing.JPanel {


    @Override
     public void paintComponent(Graphics g) {
        UIDefaults uid = UIManager.getDefaults();
        Graphics2D g2d = (Graphics2D)g;

        int w = getWidth();
        int h = getHeight();

        Color lightBlue = new Color(41, 117, 200);
        Color darkBlue = new Color(2, 47, 106);

        if (!isOpaque()) {
            super.paintComponent( g );
            return;
        }

        GradientPaint gp = new GradientPaint(0, 0, lightBlue, 0, h, darkBlue );

        g2d.setPaint(gp);
        g2d.fillRect( 0, 0, w, h );

        setOpaque( false );
        super.paintComponent( g );
        setOpaque( true );
    }
}

This doesn’t seem to change the objects background at all. I’m fairly new to messing with things that aren’t related to the Gui defaults.

I used the Gui builder in Netbeans to create the object, so initComponents() is also in the class, but I posted only the source that is relevant to the question.

Perhaps someone can point me in the right direction?

  • 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-21T23:41:35+00:00Added an answer on May 21, 2026 at 11:41 pm

    If you want a background JPanel to use a gradient paint, then just use it. Don’t do all that funny stuff in your code with setOpaque and super.paintComponent. e.g.,

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class GradientPaintPanel extends JPanel {
       private static final Color LIGHT_BLUE = new Color(41, 117, 200);
       private static final Color DARK_BLUE = new Color(2, 47, 106);
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          GradientPaint gradPaint = new GradientPaint(0, 0, LIGHT_BLUE, 0, getHeight(), DARK_BLUE);
          g2.setPaint(gradPaint);
          g2.fillRect(0, 0, getWidth(), getHeight());
       }
    
       public GradientPaintPanel() {
    
       }
    
       private static void createAndShowUI() {
          GradientPaintPanel gradPaintPanel = new GradientPaintPanel();
          gradPaintPanel.setPreferredSize(new Dimension(400, 300));
          JFrame frame = new JFrame("GradientPaintEg");
          frame.getContentPane().add(gradPaintPanel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code I have a view object that is an instance of
I have a view for my customer object that allows him to choose a
I have two views (JPanel) that uses the same domain object. My domain object
I have a view that I pass a viewmodel object to that contains an
I have a custom Grid view object that inherits from the base System.Web.UI.WebControls.GridView. The
I have a view that displays object information when the correct URL is provided
Assume some domain and view objects (that have no common base class) public class
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
Is it possible to have multiple view of the same display object? (e.g. same-computer
I have two UIImageView objects inside my view (both 320x480 one above the other).

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.