Java - Add Gradient Headers to JTable

How to Create a Gradient JTable Headers In Java Netbeans

JTable with Gradient Header In Java


In this Java Tutorial we will see how to create a JTable with a custom gradient header
The gradient effect is created by making a custom TableCellRenderer and overriding the paintComponent method to paint a gradient background.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author 1BestCsharp
 */
public class TableGradientHeader extends JFrame{

    
    public TableGradientHeader(){
        setTitle("Table Gradient Header");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] columnNames = {"Column 1","Column 2","Column 3"};
        Object[][] data = {
            {"Data 1", "Data 2", "Data 3"},
            {"Data 4", "Data 5", "Data 6"},
            {"Data 1", "Data 2", "Data 3"},
            {"Data 4", "Data 5", "Data 6"},
            {"Data 1", "Data 2", "Data 3"},
            {"Data 4", "Data 5", "Data 6"},
            {"Data 1", "Data 2", "Data 3"},
        };
        
        JTable table = new JTable(data,columnNames);
        JTableHeader header = table.getTableHeader();
        header.setPreferredSize(new Dimension(header.getWidth(), 75));
        
        header.setDefaultRenderer(new GradientHeaderRenderer());
        
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
        
        setSize(400,400);
        setLocationRelativeTo(null);
        
    }
    
    
    public static void main(String[] args) {
        TableGradientHeader frame = new TableGradientHeader();
        frame.setVisible(true);
    }
    
    
    // Create a custom TableCellRenderer for the table header
    class GradientHeaderRenderer extends JLabel implements TableCellRenderer{

        /*
        private final Color startColor = Color.GREEN;
        private final Color endColor = Color.BLACK;
        */
        
        private final Color startColor = Color.CYAN;
        private final Color endColor = Color.BLUE;
        
        public GradientHeaderRenderer(){
           
            setHorizontalAlignment(SwingConstants.CENTER);
            setForeground(Color.WHITE);
            
            setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 2, 2, Color.GRAY),
                    BorderFactory.createEmptyBorder(2,5,2,5))
            );
            
        }
        
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        
            setText(value.toString());
            return this;
            
        }
        
        @Override
        protected void paintComponent(Graphics g){
            // Cast Graphics to Graphics2D for more advanced rendering
            Graphics2D g2d = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            // Create a gradient paint for the background
            GradientPaint gradientPaint = new GradientPaint(0, 0, startColor,width,0, endColor);
            // Set the paint and fill the background with the gradient
            g2d.setPaint(gradientPaint);
            g2d.fillRect(0, 0, width, height);
            // Call the super method to paint the text and other components        
            super.paintComponent(g);
        }
        
        
    }
    
}



The Final Result:

JTable With A Gradient Header In Java NetBeans 1

JTable With A Gradient Header In Java NetBeans 2

JTable With A Gradient Header In Java NetBeans 3




Share this

Related Posts

Previous
Next Post »