Tuesday, May 9, 2017

Set JTable header background, foreground and border

Setting a JTable header backgound color is similar to setting a JTable column background, both are done through the TableCellRenderer.

JTable table = new JTable(model);
table.getTableHeader().setDefaultRenderer(new MyTableHeaderRenderer());

public class MyTableHeaderRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value,
                boolean isSelected,
                boolean hasFocus,
                int row, int col) {

            Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
         
            //Set the header of column 3 to a particular color
            if (col == 3) {
                comp.setBackground(Color.darkGray);
                comp.setForeground(Color.WHITE);
            } else {
                comp.setBackground(new Color(250,250,250));
                comp.setForeground(Color.BLACK);
            }

            //Set all headers with a border
            ((JLabel)comp).setBorder(BasicBorders.getInternalFrameBorder());

            return comp;
        }
    }

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment