Java - Add Images to JTable Header

How to Add Icons to JTable Headers In Java Netbeans

Add Images to JTable Header In Java


In this Java Tutorial we will see How To Create a JFrame containing a JTable with custom icon headers for each column 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.Component;
import java.awt.Dimension;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultTreeCellRenderer;

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

    public JTableIconHeader(){
        setTitle("JTable Icon Header");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        String[] columnNames = {"","",""};
        Object[][] data = {
                {"Product A", "$100", "Category 1"},
                {"Product B", "$200", "Category 2"},
                {"Product C", "$150", "Category 3"},
                {"Product D", "$200", "Category 4"},
                {"Product E", "$150", "Category 3"},
                {"Product F", "$200", "Category 2"},
                {"Product CC", "$150", "Category 1"},
                {"Product BA", "$200", "Category 4"},
                {"Product AC", "$150", "Category 1"},
                {"Product CB", "$200", "Category 2"},
                {"Product DC", "$150", "Category 4"},
                {"Product AF", "$200", "Category 2"},
                {"Product EB", "$150", "Category 3"}
        };
        JTable table = new JTable(data,columnNames);
        
        TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(0).setHeaderRenderer(new IconHeaderRenderer("images/item100.png"));
        columnModel.getColumn(1).setHeaderRenderer(new IconHeaderRenderer("images/price100.png"));
        columnModel.getColumn(2).setHeaderRenderer(new IconHeaderRenderer("images/stock100.png"));
        
        
        table.setRowHeight(27);
        table.setBackground(Color.GRAY);
        table.setForeground(Color.WHITE);
        table.setGridColor(Color.YELLOW);
        
        JScrollPane scrollPane = new JScrollPane(table);
        
        add(scrollPane);
        setSize(700,500);
        setLocationRelativeTo(null);
        
    }
    
    public static void main(String[] args) {

        JTableIconHeader frame = new JTableIconHeader();
        frame.setVisible(true);
        
    }
    
}


// Create a custom cell renderer class for rendering headers with icons
class IconHeaderRenderer extends DefaultTableCellRenderer{
    
    private ImageIcon icon;
    
    // Constructor that takes the name of the icon file
    public IconHeaderRenderer(String iconName){
        // Retrieve the URL of the icon file
        URL imageURL = JTableIconHeader.class.getResource(iconName);
        
        // If the URL is not null, create an ImageIcon
        if(imageURL != null){ icon = new ImageIcon(imageURL); }
        
        // Set the horizontal alignment of the cell renderer to center
        setHorizontalAlignment(CENTER);
        setBackground(Color.WHITE);
        setForeground(Color.DARK_GRAY);
    }
    
    
    // Override method to customize the rendering of table cells
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
        
        // Get the default label component for rendering
        JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        
        // If the icon is not null, set the icon, size, and border for the label
        if(icon != null){
            label.setIcon(icon);
            label.setPreferredSize(new Dimension(150, 150));
            label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
        }
        
        // Return the customized label
        return label;
        
    }
    
    
}



The Final Result:

How to Add Icons to JTable Headers In Java

How to Add Images to JTable Headers In Java







Share this

Related Posts

Previous
Next Post »