Java - JTable with Gradient Background

How to Customize JTable Background with Gradient Colors In Java Netbeans

JTable with Gradient Background


In this Java Tutorial we will see How To Create a JTable with a gradient background In Java Using Netbeans.

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.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.plaf.basic.BasicTableUI;

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

    public GradientBackgroundTable(){
        
        setTitle("Gradient Background Table");
        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"},
            {"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"},
            {"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"},
            {"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"},
            {"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"},
            {"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"},
            {"Data 4", "Data 5", "Data 6"},{"Data 1", "Data 2", "Data 3"},
            {"Data 4", "Data 5", "Data 6"}
        };
        JTable table = new JTable(data,columnNames);
        table.setUI(new GradientTableUI());
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
        setSize(400,300);
        setLocationRelativeTo(null);
        
    }
    
    public static void main(String[] args) {
        GradientBackgroundTable frame = new GradientBackgroundTable();
        frame.setVisible(true);
    }
    
    
}


class GradientTableUI extends BasicTableUI{
    
    @Override
    public void paint(Graphics g, JComponent c){
        JTable gTable = (JTable)c;
        Graphics2D g2d = (Graphics2D) g;
        
        // Create a gradient paint for the background
        Point2D startPoint = new Point2D.Float(0, 0);
        Point2D endPoint = new Point2D.Float(0, gTable.getHeight());
        Color color1 = Color.green;
        Color color2 = Color.red;
        GradientPaint gradientPaint = new GradientPaint(startPoint, color1, endPoint, color2);
        
        // Set the gradient paint and fill the entire background
        g2d.setPaint(gradientPaint);
        g2d.fillRect(0, 0, gTable.getWidth(), gTable.getHeight());
        // Paint the rest of the table components
        super.paint(g2d, c);

    }
    
}



The Final Result:

Java JTable with Gradient Background 1

Java JTable with Gradient Background 2

Java JTable with Gradient Background 3










Share this

Related Posts

Previous
Next Post »