1 package edu.jiangxin.apktoolbox.swing.treetable;
2
3 import java.awt.Component;
4 import java.awt.Graphics;
5
6 import javax.swing.JTable;
7 import javax.swing.JTree;
8 import javax.swing.table.TableCellRenderer;
9 import javax.swing.tree.TreeModel;
10
11
12 public class MyTreeTableCellRenderer extends JTree implements TableCellRenderer {
13
14 protected int visibleRow;
15
16 private MyTreeTable treeTable;
17
18 public MyTreeTableCellRenderer(MyTreeTable treeTable, TreeModel model) {
19 super(model);
20 this.treeTable = treeTable;
21
22
23
24
25 setRowHeight(getRowHeight());
26 }
27
28
29
30
31 public void setRowHeight(int rowHeight) {
32 if (rowHeight > 0) {
33 super.setRowHeight(rowHeight);
34 if (treeTable != null && treeTable.getRowHeight() != rowHeight) {
35 treeTable.setRowHeight(getRowHeight());
36 }
37 }
38 }
39
40
41
42
43 public void setBounds(int x, int y, int w, int h) {
44 super.setBounds(x, 0, w, treeTable.getHeight());
45 }
46
47
48
49
50 public void paint(Graphics g) {
51 g.translate(0, -visibleRow * getRowHeight());
52
53 super.paint(g);
54 }
55
56
57
58
59 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
60 if (isSelected)
61 setBackground(table.getSelectionBackground());
62 else
63 setBackground(table.getBackground());
64
65 visibleRow = row;
66 return this;
67 }
68 }