+ Reply to Thread
Results 1 to 2 of 2

Thread: change color of single cell in jTable

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    13

    change color of single cell in jTable

    How to change background color of a single cell in jtable?

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    14
    You need a custom TableCellRenderer like this one
    Code:
    class CustomRenderer extends DefaultTableCellRenderer 
    {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            c.setBackground(new java.awt.Color(255, 72, 72));
            return c;
        }
    }
    Then you need to associate the CellRenderer with the table like this
    Code:
    myTable.setDefaultRenderer(String.class, new CustomRenderer());
    Then you need to associate clicks on the header with changes in the TableModel.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts