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

The Archive Base Latest Questions

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

Hi how to set gradient background to column header ? I have Jtable: JTable

  • 0

Hi how to set gradient background to column header ? I have Jtable:

JTable table = new JTable(4, 5);

and try to setColor: table.getTableHeader().setBackground(Color.blue);

but with no succes color of column header is still same and these is just one color and I need gradient
thx for help

  • 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-26T07:25:02+00:00Added an answer on May 26, 2026 at 7:25 am

    Are you coding this with NetBean’s code generation or are you coding your Swing by hand?

    Have you tried creating a class that extends the JTableHeader and overrides its paintComponent method? Give it a try and in that method create a GradientPaint object or one of its variants use it to set the Graphics2D paint’s property and then call fillRect using the current dimensions of the component to fill it with gradient color.

    For example here are two ways to do this, one with a custom JTableHeader and one with a custom cell renderer that is used by the table header:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class GradientHeader {
       private static void createAndShowGui() {
          JTable table1 = new JTable();
          GradientTableHeader gradientTableHeader = new GradientTableHeader();
          gradientTableHeader.setColumnModel(table1.getColumnModel());
          table1.setTableHeader(gradientTableHeader);
    
          DefaultTableModel model = new DefaultTableModel(new Integer[][] {
                { 1, 2 }, { 3, 4 } }, new String[] { "A", "B" });
    
          table1.setModel(model);
    
          JTable table2 = new JTable(model);
          table2.getTableHeader().setDefaultRenderer(new MyCellRenderer());
    
          JPanel mainPanel = new JPanel(new GridLayout(1, 0));
          mainPanel.add(new JScrollPane(table1));
          mainPanel.add(new JScrollPane(table2));
    
          JFrame frame = new JFrame("GradientHeader");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class GradientTableHeader extends JTableHeader {
       private static final Color COLOR_1 = new Color(255, 0, 0, 240);
       private static final Color COLOR_2 = new Color(0, 0, 255, 120);
       private static final float SIDE = 50;
       private GradientPaint gradientPaint = new GradientPaint(0, 0, COLOR_1, SIDE,
             SIDE, COLOR_2, true);
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setPaint(gradientPaint);
          g2.fillRect(0, 0, getWidth(), getHeight());
       }
    }
    
    class MyCellRenderer extends JPanel implements TableCellRenderer {
       private static final Color COLOR_1 = new Color(255, 0, 0, 200);
       private static final Color COLOR_2 = new Color(0, 0, 255, 200);
       private static final float SIDE = 50;
       private GradientPaint gradientPaint = new GradientPaint(0, 0, COLOR_1, SIDE,
             SIDE, COLOR_2, true);
       private JLabel label = new JLabel();
    
       public MyCellRenderer() {
          setOpaque(true);
          setLayout(new BorderLayout());
          add(label, BorderLayout.CENTER);
          label.setHorizontalAlignment(SwingConstants.CENTER);
       }
    
       @Override
       public Component getTableCellRendererComponent(JTable table, Object value,
             boolean isSelected, boolean hasFocused, int arg4, int arg5) {
          label.setText(value.toString());
          return this;
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setPaint(gradientPaint);
          g2.fillRect(0, 0, getWidth(), getHeight());
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set a custom gradient color background for my nav bar. I want
I am trying to set the gradient background color for my panel via skins.
I want to create a UILabel and set its background to the gradient-blue like
Possible Duplicate: How do I programmatically set the background color gradient on a Custom
I try to use the GradientDrawable to set a gradient to some backgrounds and
In css you may normally set both a background-image, and a background-color, and the
I'm try to render a gradient background on some form elements. While it works
I have an issue when attempting to set the background of a grid in
I have a cross-browser CSS gradient, such as this: #background { background: #1E5799; /*
I'm trying to set a gradient background on a view, but my code below

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.