| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.bss; |
| 27 |
|
| 28 |
import java.awt.Component; |
| 29 |
|
| 30 |
import javax.swing.DefaultListCellRenderer; |
| 31 |
import javax.swing.JComboBox; |
| 32 |
import javax.swing.JList; |
| 33 |
import javax.swing.JTable; |
| 34 |
import javax.swing.table.TableCellRenderer; |
| 35 |
|
| 36 |
|
| 37 |
@SuppressWarnings("serial") |
| 38 |
public class JTableComboBoxCellRenderer extends JComboBox implements |
| 39 |
TableCellRenderer { |
| 40 |
|
| 41 |
private int columnIndex; |
| 42 |
|
| 43 |
private DefaultListCellRenderer comboBoxRenderer = new DefaultListCellRenderer() { |
| 44 |
|
| 45 |
@Override |
| 46 |
public Component getListCellRendererComponent(JList list, Object value, |
| 47 |
int index, boolean isSelected, boolean cellHasFocus) { |
| 48 |
|
| 49 |
super.getListCellRendererComponent(list, value, index, isSelected, |
| 50 |
cellHasFocus); |
| 51 |
|
| 52 |
if (value != null) { |
| 53 |
|
| 54 |
switch (columnIndex) { |
| 55 |
|
| 56 |
case PartitionTableModel.DATA_INDEX: |
| 57 |
|
| 58 |
TreesTableRecord record = (TreesTableRecord) value; |
| 59 |
this.setText(record.getName()); |
| 60 |
break; |
| 61 |
|
| 62 |
default: |
| 63 |
break; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
return this; |
| 70 |
} |
| 71 |
|
| 72 |
}; |
| 73 |
|
| 74 |
public JTableComboBoxCellRenderer(int columnIndex) { |
| 75 |
|
| 76 |
super(); |
| 77 |
setOpaque(true); |
| 78 |
this.setRenderer(comboBoxRenderer); |
| 79 |
this.columnIndex = columnIndex; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
public Component getTableCellRendererComponent(JTable table, Object value, |
| 84 |
boolean isSelected, boolean hasFocus, int row, int column) { |
| 85 |
|
| 86 |
if (isSelected) { |
| 87 |
|
| 88 |
this.setForeground(table.getSelectionForeground()); |
| 89 |
this.setBackground(table.getSelectionBackground()); |
| 90 |
|
| 91 |
} else { |
| 92 |
|
| 93 |
this.setForeground(table.getForeground()); |
| 94 |
this.setBackground(table.getBackground()); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
setSelectedItem(value); |
| 100 |
|
| 101 |
if (value != null) { |
| 102 |
removeAllItems(); |
| 103 |
addItem(value); |
| 104 |
} |
| 105 |
|
| 106 |
return this; |
| 107 |
} |
| 108 |
} |