11package hk .microos .tools ;
22
3+ import java .awt .Color ;
4+ import java .awt .Component ;
35import java .util .ArrayList ;
46import java .util .Set ;
57
68import javax .swing .DefaultListSelectionModel ;
79import javax .swing .JTable ;
810import javax .swing .ListSelectionModel ;
11+ import javax .swing .table .DefaultTableCellRenderer ;
912import javax .swing .table .DefaultTableModel ;
1013import javax .swing .table .TableColumn ;
1114
@@ -16,6 +19,7 @@ public class TableHelper {
1619 public DefaultTableModel tm ;
1720 private ArrayList <String > rowStringList ;
1821 private int staticRowNum = 0 ;
22+
1923 public TableHelper (JTable table ) {
2024 this .table = table ;
2125 this .tm = (DefaultTableModel ) table .getModel ();
@@ -46,41 +50,56 @@ public void setColSize(int[] colSize) {
4650 tc .setPreferredWidth (colSize [i ]);
4751 }
4852 }
49-
50- public void fillRightTable (ArrayList <String > staticCoords ,ArrayList <String > coords ) {
53+
54+ public void fillRightTable (ArrayList <String > staticCoords , ArrayList <String > coords ) {
5155 clearAll ();
52-
53- //fill ellipses list
54- //id, mja, min, angle, x, y
56+ // fill ellipses list
57+ // id, mja, min, angle, x, y
5558 int id = 1 ;
5659 rowStringList = new ArrayList <>();
57-
58- if (staticCoords != null ){
60+
61+ if (staticCoords != null ) {
62+
5963 rowStringList .addAll (staticCoords );
6064 staticRowNum = staticCoords .size ();
61- for (String c : rowStringList ){
62- c = String .format ("%d,%s" , id , c );
63- String [] splitStr = c .split ("," );
64- tm .addRow (splitStr );
65- id ++;
66- }
67- if (coords != null ){
68- rowStringList .addAll (coords );
69- for (String c : coords ){
65+ table .setSelectionBackground (Color .RED );
66+ table .setDefaultRenderer (Object .class , new DefaultTableCellRenderer () {
67+ @ Override
68+ public Component getTableCellRendererComponent (JTable table , Object value , boolean isSelected ,
69+ boolean hasFocus , int row , int column ) {
70+
71+ final Component c = super .getTableCellRendererComponent (table , value , isSelected , hasFocus , row ,
72+ column );
73+ if (!isSelected ) {
74+ c .setBackground (row < staticRowNum ? Color .YELLOW : Color .WHITE );
75+ }
76+
77+ return c ;
78+ }
79+ });
80+ for (String c : rowStringList ) {
7081 c = String .format ("%d,%s" , id , c );
7182 String [] splitStr = c .split ("," );
7283 tm .addRow (splitStr );
7384 id ++;
7485 }
86+ if (coords != null ) {
87+ rowStringList .addAll (coords );
88+ for (String c : coords ) {
89+ c = String .format ("%d,%s" , id , c );
90+ String [] splitStr = c .split ("," );
91+ tm .addRow (splitStr );
92+ id ++;
93+ }
94+ }
7595 }
76- }
77-
96+
7897 }
7998
8099 public void fillLeftRows (Set <String > list ) {
81- //list is path string
100+ // list is path string
82101 clearAll ();
83-
102+
84103 // fill image List
85104 // "id","Image name", "#Marks","Path prefix"
86105 int id = 1 ;
@@ -108,23 +127,27 @@ public String getBehindRowDataAt(int row) {
108127 }
109128
110129 public void setValueAt (int row , int col , Object v ) {
111- if (row > this .tm .getRowCount ())
112- this .tm .setRowCount (row + 1 );
130+ if (row > this .tm .getRowCount ())
131+ this .tm .setRowCount (row + 1 );
113132 tm .setValueAt (v , row , col );
114133 }
115- public void clearAll (){
134+
135+ public void clearAll () {
116136 for (int i = 0 ; i < tm .getRowCount (); i ++) {
117137 tm .removeRow (i );
118138 }
119139 tm .setRowCount (0 );
120140 }
141+
121142 public void setSelectedRow (int row ) {
122143 if (row >= tm .getRowCount ())
123144 return ;
124145 this .table .getSelectionModel ().setSelectionInterval (row , row );
125146 }
126- public int getRowIndexOfValue (String v ){
127- if (rowStringList == null ) return -1 ;
147+
148+ public int getRowIndexOfValue (String v ) {
149+ if (rowStringList == null )
150+ return -1 ;
128151 return rowStringList .indexOf (v );
129152 }
130153}
0 commit comments