View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.gui.table;
22  
23  import java.awt.Color;
24  import java.awt.Component;
25  
26  import javax.swing.JTable;
27  import javax.swing.SwingConstants;
28  import javax.swing.UIManager;
29  import javax.swing.table.DefaultTableCellRenderer;
30  import javax.swing.table.JTableHeader;
31  
32  /**
33   * Cell renderer changing foreground and background colors.
34   */
35  public class GroupableTableCellRenderer extends DefaultTableCellRenderer {
36  	
37  	/**
38  	 * 
39  	 */
40  	private static final long serialVersionUID = -3785644336203425095L;
41  	private Color foregroundColor = Color.WHITE;
42  	private Color backgroundColor = Color.BLUE;
43  	
44  	public GroupableTableCellRenderer() {
45  		
46  	}
47  	
48  	public GroupableTableCellRenderer(Color foregroundColor, Color backgroundColor) {
49  		this.foregroundColor = foregroundColor;
50  		this.backgroundColor = backgroundColor;
51  	}
52  	
53  	/**
54  	 * 
55  	 * @param table
56  	 * @param value
57  	 * @param selected
58  	 * @param focused
59  	 * @param row
60  	 * @param column
61  	 * @return
62  	 */
63  	public Component getTableCellRendererComponent(JTable table, Object value,
64  			boolean selected, boolean focused, int row, int column) {
65  		JTableHeader header = table.getTableHeader();
66  		if (header != null) {
67  			setForeground(foregroundColor);
68  			setBackground(backgroundColor);
69  		}
70  		setHorizontalAlignment(SwingConstants.CENTER);
71  		setText(value != null ? value.toString() : " ");
72  		setBorder(UIManager.getBorder("TableHeader.cellBorder"));
73  		return this;
74  	}
75  }