1

I tried to wrote custom renderer, but fail to activate it's features:

public class HeaderRenderer extends JPanel implements TableCellRenderer {

        private static final long serialVersionUID = -1247350397731719795L;

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

            int modelColumn = table.convertColumnIndexToModel(column);
            /*
            if( table.getSelectedColumn() == modelColumn ) {
                isSelected = true;
            }
            */

            JComponent ans;

            if (modelColumn == 0) {
                ans = new JLabel("<html><pre>" + getColumnName(column) + "</pre></html>");
            }
            else {
                ans = new JLabel("<html><pre>" + String.valueOf(directory.getCorpus(modelColumn - 1)) + "</pre></html>");

                table.getColumnModel().getColumn(table.convertColumnIndexToView(column)).setPreferredWidth(300);
            }

            //ans.setBorder(BorderFactory.createLineBorder(Color.black));

            ((JLabel) ans).setFont(new Font("SansSerif", Font.PLAIN, 10));
            ans.setForeground(Color.BLUE);
            //((JLabel)ans).setBackground(Color.yellow);
            //ans.setBorder(BorderFactory.createEtchedBorder());
            ans.setBorder(BorderFactory.createLineBorder(Color.black));

            if (isSelected) {
                ans.setBackground(UIManager.getColor("Table.selectionBackground"));
            }
            else {
                ans.setBackground(getBackground());
            }

            ((JLabel) ans).setVerticalAlignment(SwingConstants.TOP);

            JTableHeader header = table.getTableHeader();
            Dimension dim = new Dimension();

            if (ans.getPreferredSize().height > dim.height) {
                dim.height = ans.getPreferredSize().height;
                header.setPreferredSize(dim);
            }

            return ans;

        }

    }

Apparently, header is not repainted upon cell selection, so renderer is not run.

How to force it to run?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • I would love to know why some people downvote without even commenting. – Menelaos Dec 04 '14 at 23:58
  • Take a look at BOTH these answers to this [question](http://stackoverflow.com/questions/15489818/highlighting-a-column-header-of-a-jtable/15490116#15490116) – MadProgrammer Dec 05 '14 at 01:22

0 Answers0