Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

particular one table header color java swing

Tags:

swing

I want to change the background color of particular table header. In my appliaction I have to set header color Red on the current month. enter image description here My Code is here::

     jTable1.getTableHeader().
    setDefaultRenderer(
    new DefaultTableHeaderCellRenderer());



  @Override
  public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
    super.getTableCellRendererComponent(table, value,
            isSelected, hasFocus, row, column);
    JTableHeader tableHeader = table.getTableHeader();

    if(column==1)
    tableHeader.setBackground(Color.red);


    return this;
  }

this make all the header color's red. Please give me some suggestion. Thanks in advance.

like image 531
Aritra Avatar asked Mar 23 '12 10:03

Aritra


1 Answers

The infamous color memory of DefaultTableCellRenderer :-) You have to

  • set the background color always: that is for both normal and highlighted state
  • do so before calling super

something like:

  @Override
  public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row,
        int column) {
      if (myHighlightCondition) {
          setBackground(Color.RED);
      } else {
          setBackground(null);
      }
     super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
           row, column);
     return this;
  }

For more details (and why that's needed) see a How do I correctly use custom renderers to paint specific cells in a JTable?

like image 192
kleopatra Avatar answered Sep 27 '22 17:09

kleopatra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!