View Javadoc

1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
17   */
18  package org.jadira.dependencynavigator.gui;
19  
20  import java.awt.Color;
21  import java.awt.Component;
22  import java.awt.Font;
23  
24  import javax.swing.JTable;
25  import javax.swing.table.DefaultTableCellRenderer;
26  
27  public class SelectableTableModelRenderer extends DefaultTableCellRenderer {
28  
29      private static final long serialVersionUID = 5386816137675796795L;
30  
31      @Override
32      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
33          DefaultTableCellRenderer r = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
34          SelectableTableModel model = (SelectableTableModel) table.getModel();
35          r.setForeground(Color.BLACK);
36          if (model.isSelected(row)) {
37              r.setFont(r.getFont().deriveFont(Font.BOLD));
38              r.setBackground(Color.YELLOW);
39              r.setOpaque(true);
40          } else {
41              r.setFont(r.getFont().deriveFont(Font.PLAIN));
42              r.setBackground(Color.WHITE);
43              r.setOpaque(false);
44          }
45          return r;
46      }
47  }