javaswingjtable

Problem Drawing JLabel as Column Header in JTable (and Overriding paintComponent)


I am trying to use a JLabel for column headers in a JTable, and override paintComponent to color a percentage of non-null values for each column, kind of like a background visualization of a fill rate. It seems to be quite straightforward, but for some reason visualization is always applied to the first (left) column only, like in the picture below:

Example

The "fill rate" is just random in the example, but only applied for the first column although looping through all columns.

Edit: Using Java 8 and MacOS

The full source code is:

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.util.Random;

public class CustomTable extends JFrame {

    private static class CustomLabel extends JLabel {

        public CustomLabel(String text) {
            super(text);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Random random = new Random(System.currentTimeMillis());
            int w = random.nextInt(getWidth());
            int h = getHeight();
            int x = getX();
            int y = getY();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.green);
            g2.fillRect(x, y, w, h);
            super.paintComponent(g2);
            g2.dispose();
        }

    }

    private static class CustomTableCellRenderer implements TableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            return ((CustomLabel) value);
        }

    }

    public CustomTable() {
        setTitle("CustomTable");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(320, 240);
        setLayout(new GridLayout(1, 1));
        String[] columns = {"id", "fname", "lname"};
        String[][] data = {{"1", "John", "Doe"}, {"2", "Jane", "Doe"}};
        JTable jt = new JTable(data, columns);
        TableColumnModel model = jt.getColumnModel();
        for (int i = 0; i < columns.length; i++) {
            CustomLabel cl = new CustomLabel(columns[i]);
            model.getColumn(i).setHeaderValue(cl);
            model.getColumn(i).setHeaderRenderer(new CustomTableCellRenderer());
        }
        add(new JScrollPane(jt));
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                } catch (Exception e) {
                }
                new CustomTable();
            }
        });
    }

}

Solution

  • Painting of a Swing component should always be done relative the to component, not the location of the component.

    So custom painting should always have an x/y location of (0, 0), if you want to paint from the top/left of the component.

            int x = getX();
            int y = getY();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.green);
            g2.fillRect(x, y, w, h);
    

    Should be:

            //int x = getX();
            //int y = getY();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.green);
            //g2.fillRect(x, y, w, h);
            g2.fillRect(0, 0, w, h);
    

    Also, I assume you are just using the "random" method for the simplicity of the example code. As suggested above in the comment you should not be using random in a painting method since the paintComponent method is called every time a cell needs to be repainted. For example, resize the width of the frame to see the random results.